[英]Determine if app was launched from home screen?
In my main activity, onCreate I set a textView to the current date. 在我的主要活动onCreate中,我将textView设置为当前日期。 From that activity, I startActivityForResult activity2, where I can change the date. 从该活动中,我启动ActivityForResult activity2,可以在其中更改日期。 onActivityResult, I set the textView to whatever date was returned. onActivityResult,我将textView设置为返回的任何日期。
Now, if you close the main activity and the textView is not the current date. 现在,如果您关闭主要活动并且textView不是当前日期。 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. 下次(从主屏幕或应用程序抽屉上的按钮)打开它时,我希望将其设置为当前日期。
After it opens for the first time, the next time you open it, it doesn't hit onCreate. 第一次打开后,下次打开时,它不会按onCreate。 I've tried using onResume however this prevents activity2 onActivityResult from changing the date. 我尝试使用onResume,但是这会阻止activity2 onActivityResult更改日期。
Try to save the value of the date from activity2 into the android SharedPreferences. 尝试将activity2中的日期值保存到android SharedPreferences中。
You can set the date to the preferences by using something like this: 您可以使用以下方式将日期设置为首选项:
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString(MY_NAME, "Sai");
prefsEditor.putString(MY_WALLPAPER, "f664.PNG");
prefsEditor.commit();
and you can read the preferences by using something like that: 您可以使用类似的方法来阅读首选项:
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString(MY_NAME, "nothing");
String wallPaper = myPrefs.getString(MY_WALLPAPER, null);
Set a flag like "dateWasAlreadySet" to the SharedPreferences if the date was set from activty2, so you know that the date was already set. 如果日期是从activty2设置的,则将诸如“ dateWasAlreadySet”的标志设置给SharedPreferences,这样您就知道日期已经设置。
Pseudo Code: 伪代码:
If(SharedPreferences.dateWasAlreadySet){
activity1.textField.text = SharedPreferences.get("myDate");
} else {
activity1.textField.text = currentDate;
}
I hope you understand what I mean. 我希望你明白我的意思。
How about something like this: 这样的事情怎么样:
First of all, create an onSaveInstanceState(Bundle)
method and save the current contents of the date TextView
to it. 首先,创建一个onSaveInstanceState(Bundle)
方法并将日期TextView
的当前内容保存到其中。 This is called before Android stops, hides, or destroys your activity. 这是在Android停止,隐藏或破坏您的活动之前调用的。
When you start activity2, your onStop()
of activity1 will be called. 当您启动活动2时,将onStop()
您的活动1的onStop()
。 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). 在其中设置一个标志,以指示由于启动新活动而导致onStop()
是否被调用(使activity2的意图成为成员,而不是为此的局部变量)。 Ensure you save this flag in onSaveInstanceState
of activity1. 确保将此标志保存在activity1的onSaveInstanceState
中。
Implement onRestoreInstanceState(Bundle)
in activity1, and restore to local variables the date and flag values. 在activity1中实现onRestoreInstanceState(Bundle)
,然后将日期和标志值还原到本地变量。
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. 最后,在onResume
中,检查该标志-您正在恢复该标志,因为您要从activity2返回,请不要更改TextView,因为它将在onActivityResult
设置,否则,可以安全地恢复保存的日期值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.