在 Android 中恢复活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12408719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:35:37  来源:igfitidea点击:

Resume activity in Android

androidandroid-activityresumeonstart

提问by Sárzena

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));

回答by ThePCWizard

If your Activityis 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);

回答by Spike777

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:

您应该使用以下方法捕获调用方 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

希望它有帮助,问候

回答by kingraam

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:

您想要的启动模式标志是 clearTop。这将返回主活动的前一个实例,并从活动堆栈中清除第二个和第三个活动。例如,要从代码中执行此操作:

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

回答by Gan

You startActivityForResult instead of startActivity.

你 startActivityForResult 而不是 startActivity。

refer the android dev for more info here.

请参阅此处的 android 开发人员了解更多信息。