在上次使用的屏幕上打开应用程序

Open application on the last screen used

每当我点击我的应用程序图标时,它总是会在登录屏幕上打开,即使我之前已经在另一个页面上打开过它,即使我只是返回到应用程序的主屏幕,点击应用程序 returns到页面登录。我想知道我是否可以保证它总是在我停止的最后一个屏幕上打开,除非我已经完全退出应用程序。

为此,您可以使用共享首选项。每次用户停止应用程序时,您应该将 activity 的名称存储在内存中,以便下次打开应用程序时,您可以读取文件并检查之前使用的 activity。 写入文件:

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);    

并且对于每个 activity 覆盖此方法:

@override
public void onDestroy() {
    Editor editor = sharedpreferences.edit();
    editor.putString("LastOpened", "activity_name");
    editor.apply();
}

要在之前销毁的 activity 中打开应用程序,请在登录 activity 中写入:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("LastOpened", "");
switch(name) {
    case "LoginActivity": {
        //do nothing
    }
    case "OtherActivity" : {
        startActivity(new Intent(this, OtherActivity.class));
    ....
}