简体   繁体   English

恢复Android中的活动

[英]Resume activity in Android

I have an app with 3 activities. 我有一个有3个活动的应用程序。

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. 我想在不进入onCreate的情况下返回主要活动。

This is the code for the third activity: 这是第三个活动的代码:

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

If your Activity is still running, this code will bring it to the front without entering onCreate 如果您的Activity仍在运行,则此代码会将其显示在前面而不输入onCreate

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

in order to get back to previous Activity you have to finish the visible one, use this: 为了回到之前的Activity,你必须完成可见的,使用这个:

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: 你应该使用以下方法在调用者Activity上捕获结果:

@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 希望它有所帮助,问候

You startActivityForResult instead of startActivity. 你startActivityForResult而不是startActivity。

refer the android dev for more info here . 请参阅更多信息Android开发人员在这里

The launch mode flag you want is clearTop. 您想要的启动模式标志是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM