Resume activity in Android

梦想的初衷 提交于 2019-11-29 00:55:36

问题


I have an app with 3 activities.

I have the main activity. This calls the second activity, which then calls the third activity. I want return to the main activity without entering the onCreate.

This is the code for the third activity:

startActivity(new Intent(TerceraActiviry.this, Main.class));

回答1:


If your Activity is still running, this code will bring it to the front without entering onCreate

Intent openMainActivity = new Intent(TerceraActiviry.this, Main.class);
openMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityIfNeeded(openMainActivity, 0);



回答2:


in order to get back to previous Activity you have to finish the visible one, use this:

finish();

If the activity was started for a result, you should give a result then, like this:

Intent intent = new Intent();
intent.putExtra(KEY_RESPONSE, responseData);
setResult(RESULT_OK, intent);
finish();

And you should catch the result on the caller Activity using:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  switch (requestCode) {
    // Test for the code you have used to start the Activity
  }
}

Hope it helps, Regards




回答3:


You startActivityForResult instead of startActivity.

refer the android dev for more info here.




回答4:


The launch mode flag you want is clearTop. This will go back to the previous instance of the main activity and clear the second and third activity off the activity stack. For example, to do this from the code:

Intent intent = new Intent(TerceraActiviry.this, Main.class));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);


来源:https://stackoverflow.com/questions/12408719/resume-activity-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!