activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主页面" android:onClick="mainClick" /> </LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.v("chendandan","MainActivity onCreate()方法"); } public void mainClick(View view){ Log.v("chendandan","mainClick 会调用startActivityForResult"); Intent intent=new Intent(this, ChildActivity.class); startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, requestCode, data); if(requestCode==1){ if(resultCode==1){ String string=data.getStringExtra("Chen"); Log.v("chendandan","获取的数据"+string); } } } @Override protected void onStart(){ super.onStart(); Log.v("chendandan","MainActivity onStart()方法"); } @Override protected void onResume(){ super.onResume(); Log.v("chendandan","MainActivity onResume()方法"); } @Override protected void onRestart(){ super.onRestart(); Log.v("chendandan","MainActivity onRestart()方法"); } @Override protected void onStop(){ super.onStop(); Log.v("chendandan","MainActivity onStop()方法"); } @Override protected void onPause(){ super.onPause(); Log.v("chendandan","MainActivity onPause()方法"); } @Override protected void onDestroy(){ super.onDestroy(); Log.v("chendandan","MainActivity onDestroy()方法"); } }
activity_child.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ChildActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="子页面" android:onClick="childClick" /> </LinearLayout>
ChildActivity.java
public class ChildActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_child); Log.v("chendandan","ChildActivity onCreate()方法"); } public void childClick(View view){ Log.v("chendandan","childClick 会调用finish();"); Intent intent=new Intent(this, MainActivity.class); intent.putExtra("chen","子页面提交给主页面的数据"); setResult(1,intent); finish(); //startActivityForResult(intent,1); } @Override protected void onStart(){ super.onStart(); Log.v("chendandan","ChildActivity onStart()方法"); } @Override protected void onResume(){ super.onResume(); Log.v("chendandan","ChildActivity onResume()方法"); } @Override protected void onRestart(){ super.onRestart(); Log.v("chendandan","ChildActivity onRestart()方法"); } @Override protected void onStop(){ super.onStop(); Log.v("chendandan","ChildActivity onStop()方法"); } @Override protected void onPause(){ super.onPause(); Log.v("chendandan","ChildActivity onPause()方法"); } @Override protected void onDestroy(){ super.onDestroy(); Log.v("chendandan","ChildActivity onDestroy()方法"); } }
运行结果: