重新启动时如何使Android应用程序返回到上次打开的活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2441203/
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 05:52:04  来源:igfitidea点击:

How to make an android app return to the last open activity when relaunched?

android

提问by JohnRock

Is it possible to configure an android app so that if a user has opened your app, launched numerous activities, then returns to the home screen and relaunches your app again, instead of going to the main activity they will instead be taken to the activity highest on the stack (the most recent activity in your app)?

是否可以配置一个 android 应用程序,以便如果用户打开您的应用程序,启动了大量活动,然后返回主屏幕并再次重新启动您的应用程序,而不是转到主要活动,他们将被带到最高活动在堆栈上(您应用程序中的最新活动)?

回答by Josef Pfleger

When launched via icon on the home screen, Android will always start the activity with the android.intent.action.MAINfilter in your AndroidManifest.xml, unless the application is already running (in which case it will obviously restore the activity on top of the stack).

当通过主屏幕上的图标启动时,Android 将始终使用 中的android.intent.action.MAIN过滤器启动 Activity AndroidManifest.xml,除非应用程序已经在运行(在这种情况下,它显然会恢复堆栈顶部的 Activity)。

To achieve what you described you can simply store the last visible activity in SharedPreferencesand have a Dispatcheractivity that starts the last activity according to the preferences.

要实现您所描述的内容,您可以简单地将最后一个可见的活动存储在其中,SharedPreferences并让一个Dispatcher活动根据首选项启动最后一个活动。

So in every activity you want to re-start automatically:

因此,在您希望自动重新启动的每个活动中:

@Override
protected void onPause() {
    super.onPause();

    SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
    Editor editor = prefs.edit();
    editor.putString("lastActivity", getClass().getName());
    editor.commit();
}

And a Dispatcheractivity similar to the following:

以及类似于以下的Dispatcher活动:

public class Dispatcher extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Class<?> activityClass;

        try {
            SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
            activityClass = Class.forName(
                prefs.getString("lastActivity", Activity1.class.getName()));
        } catch(ClassNotFoundException ex) {
            activityClass = Activity1.class;
        }

        startActivity(new Intent(this, activityClass));
    }
}

Remarks

评论

  • You could create a base class for the onPauseoverride
  • The Dispatcheractivity obviously needs to be the android.intent.action.MAINaction
  • 您可以为onPause覆盖创建一个基类
  • 调度活动显然需要在android.intent.action.MAIN行动

回答by Shayan_Aryan

It's not that complex. You just need to manipulate the manifest.

没那么复杂。您只需要操作清单。

AndroidManifest.xm

AndroidManifest.xm

<activity
     android:name=".MainActivity"
     android:alwaysRetainTaskState="true"
     android:exported="true"
     .
     .
     .

Read about the 'android:exported' & 'android:alwaysRetainTaskState' here:

在此处阅读“android:exported”和“android:alwaysRetainTaskState”:

android:exported

安卓:出口

android:alwaysRetainTaskState

android:alwaysRetainTaskState

回答by Christopher Orr

This isthe default behaviour and this question has been asked several times before: Android: keep task's activity stack after restart from HOME
Android Run application from last Activity

默认行为,此问题之前已被问过多次: Android:从最后一个活动的HOME Android 运行应用程序重新启动后保留任务的活动堆栈

Note that if you're launching your application from Eclipse, that's what breaks this default functionality. Changing your launch configuration to launch no activity should fix things.

请注意,如果您从 Eclipse 启动您的应用程序,则会破坏此默认功能。更改启动配置以启动任何活动应该可以解决问题。

However, as this behaviour was fixed in the 0.9.6 releaseof the ADT plugin for Eclipse in the past few weeks, you no longer need that workaround:

但是,由于此行为在过去几周内在 Eclipse 的 ADT 插件的0.9.6 版本中得到了修复,因此您不再需要该解决方法:

Applications launched from ADT now behave as if they were clicked from the Home screen.

从 ADT 启动的应用程序现在的行为就像是从主屏幕单击一样。