简体   繁体   English

确定是否从主屏幕启动了应用程序?

[英]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.

相关问题 如果以前是从应用程序屏幕启动的,则从主屏幕启动应用程序将在现有实例的顶部启动它,反之亦然 - Launching app from home screen will launch it on top of an existing instance if it was launched previously from the app screen, vise versa 知道网站是否从主屏幕快捷方式启动 - Know if website launched from home screen shortcut 如何检查活动是从通知启动还是从用户单击 android 中的主屏幕仪表板应用程序图标启动 - how to check whether the activity was launched from notification or from the user click on home screen dashboard app icon in android 从家庭启动时无法还原应用 - App is not being restored when launched from Home 从主屏幕启动时,位于intent.getStringExtra的NPE - NPE at intent.getStringExtra when launched from home screen 如何隐藏从自定义主屏幕启动的应用程序的状态栏 - How to hide status bar for applications launched from custom home screen 从主屏幕快捷方式启动时,应用程序未安装错误 - Application is not installed error, when launched from home screen shortcut 从我的自定义主屏幕启动时,相机和地图崩溃 - camera and map crashes when launched from my custom home screen 从主屏幕,应用程序抽屉或Play商店确定应用程序的启动位置 - Determine where app was started from e.g. Home Screen, App Drawer, or Play Store Android-从主屏幕启动时需要有条件地启动其他活动 - Android - need different activities to be launched conditionally when launched from home screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM