确定是否应用程序是从主屏幕上推出? [英] Determine if app was launched from home screen?

查看:77
本文介绍了确定是否应用程序是从主屏幕上推出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要活动中,我的onCreate设置一个TextView为当前日期。从这个活动,我startActivityForResult活性2,在那里我可以更改日期。 onActivityResult,我设置的TextView到返回任何日期。

In my main activity, onCreate I set a textView to the current date. From that activity, I startActivityForResult activity2, where I can change the date. onActivityResult, I set the textView to whatever date was returned.

现在,如果你关闭了主要活动和TextView的不是当前日期。下一次当你打开它(从主屏幕或应用程序的抽屉的按钮),我想它TextView的设置为当前日期。

Now, if you close the main activity and the textView is not the current date. The next time you open it (from the button on home screen or app drawer), I would like it to set the textView to the current date.

在它打开的第一次,下一次你打开它,它不打的onCreate。我已经使用onResume然而,这prevents活性2 onActivityResult更改日期尝试。

After it opens for the first time, the next time you open it, it doesn't hit onCreate. I've tried using onResume however this prevents activity2 onActivityResult from changing the date.

推荐答案

怎么是这样的:

首先,创建一个<一个href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29"相对=nofollow> 的onSaveInstanceState(捆绑) 方法和保存日期的当前内容的TextView 给它。这就是所谓的Andr​​oid停止之前,隐藏或破坏你的活动。

First of all, create an onSaveInstanceState(Bundle) method and save the current contents of the date TextView to it. This is called before Android stops, hides, or destroys your activity.

当你启动活性2,你的的onStop()相关活动,将被调用。在那里,设置一个标志,指示是否的onStop()被称为因你开始一个新的活动(使意图活性2的成员,这不是局部变量)。确保您保存此标志在的onSaveInstanceState 活动1中。

When you start activity2, your onStop() of activity1 will be called. In there, set a flag to indicate whether or not onStop() is called due to you starting a new activity (make the intent for activity2 a member, not local variable for this). Ensure you save this flag in onSaveInstanceState of activity1.

实施 onRestoreInstanceState(捆绑)的相关活动,并恢复到局部变量的日期和标志值。

Implement onRestoreInstanceState(Bundle) in activity1, and restore to local variables the date and flag values.

最后,在 onResume 活动1,检查的标志 - 它,你正在恢复,因为你是从活性2回,不改变TextView的,因为这将被设置 onActivityResult ,否则,您可以安全地恢复保存的日期值。

Finally, in onResume of activity1, check the flag - it you are resuming because you are returning from activity2, do not change the TextView since that will be set in onActivityResult, otherwise, you can safely restore the saved date value.

这篇关于确定是否应用程序是从主屏幕上推出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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