约定:当Activity发生跳转时将原来的Activity成为父Activity,将新出现的Activity成为子Activity。
情景设置
下面是个发短信的Activity
当我们点击图中的+按钮后会跳转到联系人界面
在联系人界面选择某个联系人后,比如联系人ccc,联系人界面消失并回到发短信界面。
这其中设计到了Activity的跳转以及数据回传
过程分析
当在父Activity中进行了某些操作后,产生了子Activity。然后我们在子Activity又进行了一些操作,同时希望父Activity知道这些操作。那么在子Activity退出时将数据封装到一个对象中,而在父Activity中接受回传的这个对象。
编程实现
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/contact" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入联系人"/> <Button android:id="@+id/add" android:layout_weight="0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:onClick="click"/> </LinearLayout> <EditText android:id="@+id/content" android:gravity="top" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" android:hint="请输入消息内容"/> <Button android:id="@+id/reply" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="快捷回复" android:onClick="click1"/> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送短信"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/item" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp"/> </LinearLayout>
java代码
package xidian.dy.com.chujia; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //点击事件发生时切换至联系人Activity public void click(View v) { Intent intent = new Intent(); intent.setClass(this, ContactActivity.class); intent.addCategory(Intent.CATEGORY_DEFAULT); //设置子Activity在销毁时会有数据返回 //10为子Activity的编号 startActivityForResult(intent, 10); } public void click1(View v) { Intent intent = new Intent(); intent.setClass(this, ReplyActivity.class); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivityForResult(intent, 20); } //当子Activity被销毁的后,父Activity会回到前台,这时候该方法就会被调用 //来获取子Activity返回给父Activity的数据 /** * * @param requestCode 在创建子Activity时为其指定的的编号 * @param resultCode 返回的数据类型(从功能上划分),一个子Activity可能返回种数据类型(如联系人姓名/联系人电话) * @param data 返回的数据 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //获取返回的数据 String name = data.getStringExtra("name"); //判断是哪个Activity返回的 if(requestCode == 10) { EditText et = (EditText) findViewById(R.id.contact); if(et != null) et.setText(name); } else if(requestCode == 20) { EditText et = (EditText) findViewById(R.id.content); if(et != null) et.setText(name); } } }
package xidian.dy.com.chujia; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; /** * Created by dy on 2016/7/8. */ public class ContactActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); ListView listView = (ListView) findViewById(R.id.lv); final String[] strs = new String[]{"aaa", "bbb", "ccc","ddd","eee","fff","ggg"}; if(listView != null) { listView.setAdapter(new ArrayAdapter<>(this, R.layout.item_list, R.id.item, strs)); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(); intent.putExtra("name", strs[position]); setResult(1, intent); finish(); } }); } } }
package xidian.dy.com.chujia; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; /** * Created by dy on 2016/7/8. */ public class ReplyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); ListView listView = (ListView) findViewById(R.id.lv); final String[] strs = new String[]{"你好", "在吗", "大海"}; if(listView != null){ //填充布局 listView.setAdapter(new ArrayAdapter<>(this, R.layout.item_list, R.id.item, strs)); //设置监听器 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent data = new Intent(); data.putExtra("name", strs[position]); //设置数据类型 setResult(1, data); //销毁对象 finish(); } }); } } }
清单文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xidian.dy.com.chujia"> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="主界面"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ContactActivity"> <intent-filter> <action android:name="android.intent.action.contact"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <activity android:name=".ReplyActivity"> <intent-filter> <action android:name="android.intent.action.replay"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> </application> </manifest>