如何启动活动只有一次,当应用程序被打开了第一次?
我有一个活动,我只希望当应用程序运行在第一次运行。
I have an activity that i only want to run when the application is ran for the first time.
和永远不会再。这是一个Facebook的登录活动。我只是想发动一次当应用程序最初开的第一次。
And never again. It is a facebook login activity. I only want to launch it once when the app is initially opened for the first time.
我要如何去这样做?
我一般都做什么是添加一个检查一个特定的共享preference在主要活动
:如果该共享preference缺少然后启动一个运行的活动,否则继续的主要活动。当您启动一次运行活动创建共享preference因此它被忽略下一次。
What I've generally done is add a check for a specific shared preference in the Main Activity
: if that shared preference is missing then launch the single-run Activity, otherwise continue with the main activity . When you launch the single run Activity create the shared preference so it gets skipped next time.
修改:在我的 onResume
默认的活动我这样做:
EDIT : In my onResume
for the default Activity I do this:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
if(!previouslyStarted) {
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
edit.commit();
showHelp();
}
基本上我加载默认共享preferences并查找 previously_started
布尔preference。如果还没有设置我将它设置,然后启动帮助文件。我用它自动显示的帮助下,第一次安装的应用程序。
Basically I load the default shared preferences and look for the previously_started
boolean preference. If it hasn't been set I set it and then launch the help file. I use this to automatically show the help the first time the app is installed.