基于startActivityForResult方法处理两个Activity之间数据传递问题

2019-12-10 19:51:38丽君

xml文件:

 <RelativeLayout xmlns:android="http://www.easck.com/apk/res/android"
   xmlns:tools="http://www.easck.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#ff" >
   <Button
     android:id="@+id/button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="dp"
     android:text="启动Activityb" />
 </RelativeLayout>
 <?xml version="." encoding="utf-"?>
 <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#ff"
   android:orientation="vertical" >
   <Button
     android:id="@+id/button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="返回Activity" />
 </LinearLayout>

别忘在AndroidManifast中注册activityb。

运行效果图:

基于startActivityForResult方法处理两个Activity之间数据传递问题

startActivityForResult与startActivity的不同之处在于:

1、startActivity( ) 

仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivity( )。

2、startActivityForResult( ) 

可以一次性完成这项任务,当程序执行到这段代码的时候,假若从T1Activity跳转到下一个Text2Activity,而当这个Text2Activity调用了finish()方法以后,程序会自动跳转回T1Activity,并调用前一个T1Activity中的onActivityResult( )方法。



注:相关教程知识阅读请移步到Android开发频道。