启动应用程序时如何启动上次打开的活动? [英] How to start the activity that is last opened when launch an application?

查看:125
本文介绍了启动应用程序时如何启动上次打开的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个包含多项活动的应用程序.随时在后堆栈中只有一个活动实例.当我从名为AcitivityOne的活动中退出应用程序时,下次如何使用ActivityOne启动该应用程序?

I designed an app with several activities. There is only an activity instance in the back stack at any time. When I quit the application from an activity named AcitivityOne, how could I launch the application with ActivityOne next time?

推荐答案

快速方法是在onCreate()中将flags放在setContentView()之后:

The fast method, is that in your onCreate() put those flags after setContentView():

if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
    finish(); 
    return; 
} 

您还可以创建一个SharedPreferences,最后一个Activity如下所示:

Also you can create a SharedPreferences with the Activity last oppened as follows :

将您的onPause()修改为此:

@Override
protected void onPause() {
  super.onPause();
  SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
  Editor editor = prefs.edit();
  editor.putString("lastopened", getClass().getName());
  editor.commit();
}

然后在onCreate()中再次输入:

Class<?> LastOpened;

    try {
        SharedPreferences prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
        LastOpened= Class.forName(prefs.getString("lastoppened", MainActivity.class.getName()));
    } catch(ClassNotFoundException ex) {
        LastOpened= MainActivity.class;
    }

    startActivity(new Intent(this, LastOpened));

如果这没有帮助,请查看以下

If it doesn't help take a look at this thread

这篇关于启动应用程序时如何启动上次打开的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆