Android开发关于onActivityResult()的执行时间问题

来源:互联网 发布:js 监听url 变化事件 编辑:程序博客网 时间:2024/05/22 07:41

当我们调用startActivityForResult()方法来跳转页面的时候需要重写onActivityResult方法,不然就和startActivity没什么两样,本文说明onActivityResult方法会在什么时候回被触发。这里我们用一段小程序来说明

<span style="font-family: Arial, Helvetica, sans-serif;">public class MainActivity extends Activity {</span>
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.i("TAG", "onCreate");Button bt=(Button) findViewById(R.id.id_bt);bt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(MainActivity.this,Aty2.class);startActivityForResult(intent, 1);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode==1) {Log.i("TAG", "onActivityResult");}super.onActivityResult(requestCode, resultCode, data);}@Overrideprotected void onStart() {super.onStart();Log.i("TAG", "onStart");}@Overrideprotected void onResume() {super.onResume();Log.i("TAG", "onResume");}@Overrideprotected void onRestart() {super.onRestart();Log.i("TAG", "onRestart");}@Overrideprotected void onStop() {super.onStop();Log.i("TAG", "onStop");}@Overrideprotected void onDestroy() {super.onDestroy();Log.i("TAG", "onDestroy");}}

public class Aty2 extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_aty2);Button button=(Button) findViewById(R.id.id_bt);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();setResult(RESULT_CANCELED);finish();}});}}

依次点击两个页面的button,Logcat中答应的日志如下:


从日志中可以看出从第二个页面返回第一个页面是的顺序依次是onActivityResult—>onRestart—>。。。。

1 0
原创粉丝点击