startActivityForResult、onActivityResult对应的A与B的Activity的生命周期

一、源码:

1.MainActivity 

package com.example.testdemo;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener {


private TextView tv_skip;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("MianActivity>>>>>>>>>测试!!!!", "onCreate!!!!!!!!!!!!!!!!!!!!!!!");
tv_skip = (TextView) findViewById(R.id.tv_skip);
tv_skip.setOnClickListener(this);

}


@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.e("MianActivity>>>>>>>>>测试!!!!", "onRestart!!!!!!!!!!!!!!!!!!!!!!!");
}


@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("MianActivity>>>>>>>>>测试!!!!", "onStart!!!!!!!!!!!!!!!!!!!!!!!");


}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("MianActivity>>>>>>>>>测试!!!!", "onResume!!!!!!!!!!!!!!!!!!!!!!!");
}


@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e("MianActivity>>>>>>>>>测试!!!!", "onPause!!!!!!!!!!!!!!!!!!!!!!!");


}


@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e("MianActivity>>>>>>>>>测试!!!!", "onStop!!!!!!!!!!!!!!!!!!!!!!!");
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("MianActivity>>>>>>>>>测试!!!!", "onDestroy!!!!!!!!!!!!!!!!!!!!!!!");
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(this, TestActivity.class);
startActivityForResult(intent, 123);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==456) {
Log.e("MianActivity>>>>>>>>>测试!!!!", "走了---onActivityResult");
}
}



}

2.package com.example.testdemo;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class TestActivity extends Activity  implements OnClickListener {



private static final String TAG = TestActivity.class.getName();
 


private TextView tv_back;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
tv_back = (TextView) findViewById(R.id.tv_back);
tv_back.setOnClickListener(this);
Log.e("TestActivity>>>>>>>>>测试!!!!", "onCreate!!!!!!!!!!!!!!!!!!!!!!!");
}


@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.e("TestActivity>>>>>>>>>测试!!!!", "onRestart!!!!!!!!!!!!!!!!!!!!!!!");
}


@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.e("TestActivity>>>>>>>>>测试!!!!", "onStart!!!!!!!!!!!!!!!!!!!!!!!");


}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("TestActivity>>>>>>>>>测试!!!!", "onResume!!!!!!!!!!!!!!!!!!!!!!!");
}


@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.e("TestActivity>>>>>>>>>测试!!!!", "onPause!!!!!!!!!!!!!!!!!!!!!!!");


}


@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.e("TestActivity>>>>>>>>>测试!!!!", "onStop!!!!!!!!!!!!!!!!!!!!!!!");
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("TestActivity>>>>>>>>>测试!!!!", "onDestroy!!!!!!!!!!!!!!!!!!!!!!!");
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
this.setResult(456);
finish();
}




}

二、结论:

      1.运行起来MainActivity生命周期:

走到onResume()

2.点击跳转a与b的生命周期变化:


3.点击返回b与a的生命周期变化:



                      



你可能感兴趣的:(android,Android开发)