简体   繁体   English

如何保存上次访问的活动并在开始时启动它

[英]How to save last visited activity and launch it on start

I have the MainActivity which is welcome activity and some other ones.我有MainActivity这是受欢迎的活动和其他一些活动。
Lets suppose Act1 , Act2 and Act3 .让我们假设Act1Act2Act3
I want the program automatically redirect from MainActivity directly to last visited activity on every app launch.我希望程序在每次应用启动时自动从MainActivity直接重定向到上次访问的活动。
whether Act1 , Act2 or Act3无论是Act1Act2还是Act3
Well, I tried to do this jump from main activity to last visited activity , but it didn't work.好吧,我尝试从主要活动跳转到上次访问的活动,但没有成功。
here's code what I've already tried and didn't work:这是我已经尝试过但没有用的代码:

MainActivity:主要活动:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadLocale();
        setContentView(R.layout.activity_main);

        SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
        int restoredLevel = prefs.getInt("level", 0);
        if (restoredLevel > 0) {
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
        }
    }

Act1:第一幕:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Act1);

        SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
        editor.putInt("level", 1);
        editor.apply();
    }

(I simply want to change launcher activity depending on last visited activity, if you know any better solution for my problem, I will be thankful) (我只是想根据上次访问的活动更改启动器活动,如果您知道我的问题有更好的解决方案,我将不胜感激)

When launched via icon on the home screen, Android will always start the activity with the android.intent.action.MAIN filter 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 将始终使用 AndroidManifest.xml 中的 android.intent.action.MAIN 过滤器启动活动。堆栈)。

To achieve what you described you can simply store the last visible activity in SharedPreferences and have a Dispatcher activity 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 Dispatcher activity similar to the following:以及类似于以下内容的 Dispatcher 活动:

public class WelcomeActivity 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 onPause override. -->您可以为 onPause 覆盖创建一个基本 class。 -->The Dispatcher activity obviously needs to be the android.intent.action.MAIN action. --> Dispatcher 活动显然需要是 android.intent.action.MAIN 动作。

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

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