应用程序的Andr​​oid杀后恢复 [英] Resume after App kill Android

查看:133
本文介绍了应用程序的Andr​​oid杀后恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计的Andr​​oid应用程序。

I am designing an Android App.

我的目标是,如果当它是在后台应用程序被杀害,并且如果用户再次启动应用程序,然后它应该有恢复应用程序的选项。

My aim is that if the App is killed while it was in the background, and if the user starts the app again then it should have the option to resume the App.

现在,我的在onStart 函数被调用在两种情况下:

Now, my onStart function is called in two scenarios:


  1. 当活动被启动首次

  2. 即使击杀后开始活动。

我要检查可在确定在onStart(),该应用程序从头开始,或者是从previous状态恢复?

I want to check which can determine in the onStart(), that the app started from scratch, or it is resuming from previous state?

推荐答案

您可以使用共享preferences像这些任务。

You can use shared preferences for tasks like these.

在你的onStop()函数,共享preferences设置一个标志,如:

In your onStop() function, set a flag in shared preferences like:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("killed", "yes");

在您在onStart(),检索preference,并检查它是这样的:

In your onStart(), retrieve the preference and check it like this:

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String flag = prefs.getString("killed", null);

if(flag!=null && flag.equals("yes")
{
//activity is resumed
}
else
{
//activity is started from scratch
}

有关共享preferences更多信息,看到这里的官方文档:
http://developer.android.com/guide/专题/数据/数据​​storage.html#preF

for more info on shared preferences, see official docs here: http://developer.android.com/guide/topics/data/data-storage.html#pref

这篇关于应用程序的Andr​​oid杀后恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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