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. 《Linear Algebra and Its Application》-chaper1-行化简法解决线性方程组

    在实际生产生活中,需要我们解大量的线性方程组,例如是有探测.线性规划.电路等,这里我们便从理论角度建立一套解决线性方程组的体系. 线性方程组: 形如下面形式的方程组称为线性方程组. 回想起解决二元线性 ...

  2. Aggregating tests in suites

    我们可以将来自不同类的test组成一个test suite.在JUnit 3.8.x我们使用 static Test suite()方法,但是在JUnit4我们使用在类前面加上注释 @RunWith( ...

  3. JavaScript String 对象实例深入研究

    本文主要介绍并分析JavaScript中String对象的具体用法,以及和String对象相关的方法,方便开发者在JavaScript开发中更好地处理字符串. 1. 介绍 String 对象,对字符串 ...

  4. 如何唯一确定一台iOS设备

    如果你的iOS应用需要针对设备做特定的操作,或者需要硬件的信息来进行判定等等的,你就需要对iOS设备进行唯一性的判定. 苹果设备有个先天的东西符合这个需求,UDID,这个东东用iTunes就可以看到, ...

  5. 转:理解Java泛型

    JDK 5.0 中增加的泛型类型,是 Java 语言中类型安全的一次重要改进.但是,对于初次使用泛型类型的用户来说,泛型的某些方面看起来可能不容易明白,甚至非常奇怪.在本月的“Java 理论和实践”中 ...

  6. tomcat : Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException:

    错误 严重: Error configuring application listener of class org.springframework.web.context.ContextLoader ...

  7. JNI- java.lang.UnsatisfiedLinkError: Native method not found

    http://stackoverflow.com/questions/24566127/jni-java-lang-unsatisfiedlinkerror-native-method-not-fou ...

  8. BlockingQueue接口

    BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,让容量满时往BlockingQueue中添加数据时会阻塞,当容量为空时取元素操作会阻塞 ...

  9. Struts2与jQuery.ajax()的结合

    1.客户端是通过$.ajax()方法向login.action传递数据:2.其中action中execute()方法返回值为空,并通过[ServletActionContext.getResponse ...

  10. activeMQ总结

    队列模式和发布订阅模式的区别 topic只有所有订阅者都消费了,这个消息才会消失.只要有一个订阅者没有消费(持久化模式),这个消息就会存在.订阅者下线然后上线也会读取到这个消息.而且队列的话,消费能力 ...