回答
我通常所做的是在Main Activity
中添加一个特定的共享首选项的检查:如果缺少该共享首选项,请启动单一运行的Activity,否则继续主要活动。当您启动单次运行时,创建共享首选项,以便下次跳过。
编辑:在我onResume
默认的活动我这样做:
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();
}
基本上我加载默认共享偏好,并寻找previously_started
布尔偏好。如果尚未设置,我将其设置,然后启动帮助文件。我使用它在第一次安装应用程序时自动显示帮助。
这样的事情可能会起作用。
public class MyPreferences {
private static final String MY_PREFERENCES = "my_preferences";
public static boolean isFirst(Context context){
final SharedPreferences reader = context.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
final boolean first = reader.getBoolean("is_first", true);
if(first){
final SharedPreferences.Editor editor = reader.edit();
editor.putBoolean("is_first", false);
editor.commit();
}
return first;
}
}
使用
boolean isFirstTime = MyPreferences.isFirst(MyActivity.this);
因此,在onCreate()我可以测试IS_FIRST布尔值,如果它继续。如果没有,然后停止并提醒用户或我想做的任何事情? –
当然你可以做到。 – Samuel
我做了这个没有共享Prefrence ......我所知共享prefrence占用一些内存,所以我用公共静态布尔变量在全局类....首先,我做全局类的AppConfig ......然后我做了布尔静态变量是这样的:
public class Appconfig {
public static boolean activity = false;
}
然后我用这个公共静态布尔变量到我的欢迎活动类。我正在使用许可证协议页面。我只能在我的应用程序中只使用一次,然后在运行应用程序时再也不显示。所以我已经把condtion在欢迎活动......如果欢迎类中运行第一次如此的静态布尔变量是假的......
if (Appconfig.activity == false) {
Intent intent = new Intent();
intent.setClass(WelcomeActivity.this,LicesnceActivity.class);
startActivity(intent);
WelcomeActivity.this.finish();
}
if (Appconfig.activity == true) {
Intent intent = new Intent();
intent.setClass(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
}
现在在Licesnce Activity类我做:
Appconfig.activity=true;
所以每当我运行应用程序的第二个活动“主要业务”欢迎活动后不运行许可证活动....
这也消耗从RAM(这是更关键),当应用程序被垃圾,这意味着该值将被设置为默认值,这是错误的,对吗? –
安置自己的onCreate语句中,下面的代码
Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("isFirstRun", true);
if (isFirstRun) {
//show start activity
startActivity(new Intent(MainActivity.this, FirstLaunch.class));
Toast.makeText(MainActivity.this, "First Run", Toast.LENGTH_LONG)
.show();
}
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
.putBoolean("isFirstRun", false).commit();
更换FirstLaunch.class与你想的类推出
如果我想清除此偏好的值,该怎么办?我可以在你的代码中做到这一点吗? – Dre
在全球
public int count=0
int tempInt = 0;
宣布在你的onCreate功能过去这段代码在第一。
count = readSharedPreferenceInt("cntSP","cntKey");
if(count==0){
Intent intent = new Intent();
intent.setClass(MainActivity.this, TemporaryActivity.class);
startActivity(intent);
count++;
writeSharedPreference(count,"cntSP","cntKey");
}
过去这两种方法以外的onCreate
//Read from Shared Preferance
public int readSharedPreferenceInt(String spName,String key){
SharedPreferences sharedPreferences = getSharedPreferences(spName,Context.MODE_PRIVATE);
return tempInt = sharedPreferences.getInt(key, 0);
}
//write shared preferences in integer
public void writeSharedPreference(int ammount,String spName,String key){
SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, ammount);
editor.commit();
}
SharedPreferences dataSave = getSharedPreferences("firstLog", 0);
if(dataSave.getString("firstTime", "").toString().equals("no")){ // first run is happened
}
else{ // this is the first run of application
SharedPreferences.Editor editor = dataSave.edit();
editor.putString("firstTime", "no");
editor.commit();
}
申报全局
public int count=0;
int tempInt = 0;
第一屏幕的OnCreate中
count = readSharedPreferenceInt("cntSP","cntKey");
if(count==0){
Intent i = new Intent(SplashScreen.this, IntroActivity.class);
startActivity(i);
count++;
writeSharedPreference(count,"cntSP","cntKey");
}
else {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
}
现在Oncreat方法
public int readSharedPreferenceInt(String spName,String key){
SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);
return tempInt = sharedPreferences.getInt(key, 0);
}
//write shared preferences in integer
public void writeSharedPreference(int ammount,String spName,String key){
SharedPreferences sharedPreferences = getSharedPreferences(spName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, ammount);
editor.commit();
}
- 1. 仅在应用程序第一次启动时启动活动
- 2. 仅启动一次活动
- 3. 如何在首次启动时仅显示一次UIPageViewController
- 4. 如何仅在首次启动时使用启动画面?
- 5. 如何使活动成为首次启动的活动,但仅限于第一次运行的应用程序
- 6. 如何仅显示一次活动一次。应该只显示第一次打开的应用程序。?
- 7. 如何在启动时仅启动一次Android视图动画?
- 8. 如何在Android上仅启动一次活动?
- 9. 仅当应用第一次启动时,如何使用特定的活动启动应用?
- 10. 初始屏幕仅在第一次启动应用程序时打开
- 11. 如何仅在首次启动应用程序时显示消息?
- 12. 如何在首次启动应用程序时显示UIViewController?
- 13. 活动打开两次
- 14. 如何在每次打开应用程序时重新启动应用程序?
- 15. 仅在新安装后启动一次的活动?
- 16. 如何在Android中启用仅退出后退活动两次?
- 17. 仅在第一次启动应用程序时显示UIAlertView
- 18. 只显示登录活动一次,并在下一次启动主要活动
- 19. Android自动启动活动(应用程序启动两次)
- 20. 仅在安装并打开应用程序时才启动活动?
- 21. Spring首次或Spring启动
- 22. iOS版 - 应用程序启动非常慢首次启动
- 23. iOS - 每次应用打开时显示启动画面
- 24. 仅通过第一次启动运行活动
- 25. 如何在启动时仅渲染一次React组件?
- 26. 在首次启动时执行应用程序指令
- 27. 如何让我的应用程序仅打开一次,下次打开应用程序时会崩溃?
- 28. HealthKit在Swift 2.0中仅启动一次
- 29. 不止一次启动活动
- 30. 如何在iPhone应用第一次启动时使UIAlertView只出现一次?
很好的建议!我会试试看!。你能提供一个小代码应该如何运作的例子吗? –
那么布尔先前开始做什么? –
那么,如果previousStarted isnt null会发生什么? –