Fem*_*emi 57
我通常做的是在以下内容中添加对特定共享首选项的检查Main 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();
}
Run Code Online (Sandbox Code Playgroud)
基本上我加载默认的共享首选项并查找previously_started布尔首选项.如果尚未设置,我将其设置,然后启动帮助文件.我使用它在第一次安装应用程序时自动显示帮助.
小智 16
在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();
Run Code Online (Sandbox Code Playgroud)
将FirstLaunch.class替换为您要启动的类
Sam*_*uel 13
这样的事可能有用.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
用法
boolean isFirstTime = MyPreferences.isFirst(CurrentActivity.this);
if (isFirstTime) {
NewActivity.show(CurrentActivity.this);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71250 次 |
| 最近记录: |