且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

确定是否应用程序是从主屏幕上推出?

更新时间:2023-02-22 11:24:17

怎么是这样的:

首先,创建一个 的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.