Info:startActivty 与 startActivityForResult区别

(1):startActivity 启动了其他Activity之后不会再回调过来,此时启动者与被启动者在启动后没有联系了。

(2):startActivityForResult 可以进行回调,之后有联系。

1:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button1"
android:layout_below="@id/tv_show"/>
</RelativeLayout>

2:MainActivity.java

 public class MainActivity extends Activity {
private Button btn1=null;
private TextView tvShow=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.btn_1); btn1.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
startActivityForResult(intent,100);
}
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); if(requestCode==100 && resultCode==Activity.RESULT_OK){
String info=data.getExtras().getString("info");
tvShow=(TextView)findViewById(R.id.tv_show);
tvShow.setText(info);
}
}
}

3:activity_other.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input info"/>

<Button
android:id="@+id/btn_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button Back"
android:layout_below="@id/et_input"/>
</RelativeLayout>

4:OtherActivity.java

 public class OtherActivity extends Activity {
private EditText etInfo=null;
private Button btnBack=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other); etInfo=(EditText)findViewById(R.id.et_input);
btnBack=(Button)findViewById(R.id.btn_back); btnBack.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent=new Intent();
String info=etInfo.getText().toString();
intent.putExtra("info", info); setResult(Activity.RESULT_OK,intent);
finish(); //关闭当前的Activity
}
});
}
}

startActivityForResult案例的更多相关文章

  1. Google最新截屏案例详解

    Google从Android 5.0 开始,给出了截屏案例ScreenCapture,在同版本的examples的Media类别中可以找到.给需要开发手机或平板截屏应用的小伙伴提供了非常有意义的参考资 ...

  2. android 案例:从另一个activity选择信息并获取返回值

    主窗口: package com.example.test; import android.app.Activity; import android.app.AlertDialog; import a ...

  3. Android(java)学习笔记222:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例的优化:请求码和结果码)

    1.开启界面获取返回值 (1)采用一种特殊的方式开启Activity:               startActivityForResult(intent , 0): (2)在被开启的Activi ...

  4. Android BLE与终端通信(五)——Google API BLE4.0低功耗蓝牙文档解读之案例初探

    Android BLE与终端通信(五)--Google API BLE4.0低功耗蓝牙文档解读之案例初探 算下来很久没有写BLE的博文了,上家的技术都快忘记了,所以赶紧读了一遍Google的API顺便 ...

  5. 22_Android中的本地音乐播放器和网络音乐播放器的编写,本地视频播放器和网络视频播放器,照相机案例,偷拍案例实现

    1 编写以下案例: 当点击了"播放"之后,在手机上的/mnt/sdcard2/natural.mp3就会播放. 2 编写布局文件activity_main.xml <Line ...

  6. WebChromeClient 简介 API 案例

    代码位置:https://github.com/baiqiantao/WebViewTest.git 设计思想理解 在WebView的设计中,不是什么事都要WebView类干的,有相当多的杂事是分给其 ...

  7. [Xamarin] 透過StartActivityForResult傳值回來(转贴)

    上一篇文章(開啟另外一個Activity 並且帶資料),提到了開啟一個新的Activity ,我們將值透過intent 帶到下個Activity 但是,如果我們開啟的Actrivity其實是有一個任務 ...

  8. Android(java)学习笔记165:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例的优化:请求码和结果码)

    1.开启界面获取返回值 (1)采用一种特殊的方式开启Activity:               startActivityForResult(intent , 0): (2)在被开启的Activi ...

  9. BaseAdapter的三种表达式分析,startActivityForResult的使用

    (一)BaseAdapter的三种表达式: ①逗比式: public View getView(int position, View convertView, ViewGroup parent) { ...

随机推荐

  1. 链接器(linker)的作用——CSAPP第7章读书笔记

    首先说说我为什么要去读这一章.这个学期开OS的课,在Morden Operating System上读到和Process有关的内容时看到这样一句话:“Process is fundamentally ...

  2. Centos6.x/Oracle11G 自动化静默安装配置脚本

    部分脚本截图如下,要想玩转联系Ruiy哥提供脚本下载路径,附件在本博客的文件栏中维护,为了避免懒人一味的索取别人的劳动成果特此如此; 想玩的Ruiy mail to you! 快6.1了,6.1娃子们 ...

  3. N - Marriage Match II - HDU 3081(最大流)

    题目大意:有一些男孩和女孩玩一个游戏,每个女孩都可以挑一个男孩来进行这个游戏(所有人都要参加),女孩只会挑选她喜欢的男孩,并且她们认为她们朋友喜欢的男孩她们也是喜欢的(朋友的男朋友也是我的男朋友??? ...

  4. CHANGE NOTEPAD DEFAULT CODE TO UTF-8

    Windows记事本新建文本文档编码默认为ANSI 修改步骤: 新建一个文本文档,不输入任何内容,然后"另存为",将编码由默认的ANSI修改为UTF-8,并将新文档命名为templ ...

  5. acm - cry for no one

    再一次回到实验室,距离上次已经四个多月了.诸多业障,三界无安,犹如火灾,众若充满,甚可怖畏,常有生老病死 忧患,如是等火,炽然不息.<成实论>云:“万法刹那生,万法刹那灭,转转相续也”.又 ...

  6. Python logging模块详解

    简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.warni ...

  7. 1033 - Merging Maps

    Pictures taken from an airplane or satellite of an ar ea to be mapped are often of sufficiently high ...

  8. Injecting and Binding Objects to Spring MVC Controllers--转

    I have written two previous posts on how to inject custom, non Spring specific objects into the requ ...

  9. Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37730469 在上一篇博客中,我们成功把地图导入了我们的项目.本篇我们准备为地图 ...

  10. NYOJ-129 并查集

    这个题基本上是并查集稍微一变, 只是加了一些判断条件而已,就是将点合并成树, 最后遍历一下, 统计一下有多少棵树, 如果不是1的话, 肯定不是树,所以,可以根据这个来判断 #include <s ...