首次启动仅显示activity
Show activity only by first start up
我有一个名为 DataActivity 的 activity,我希望它在我第一次打开我的应用程序时出现,以后再也不会出现。我希望我的 MainActivity 成为我的第一个 Activity,谁能告诉我该怎么做? (我对编码还很陌生,所以我将不胜感激......)谢谢!
亲切的问候! :)
您可以在 SharedPreferences
中保存一个条目,表明 DataActivity
已显示一次,然后在 DataActivity
onCreate()
中您可以执行以下操作:
boolean dataShownOnce = checkSharedPreferencesForDataActivityShown();
if (dataShownOnce) {
// Go to MainActivity
startActivity(new Intent(this, MainActivity.class);
finish();
} else {
// Showing DataActivity for the first time, continue with the normal logic
}
这里有一个关于使用 SharedPreferences
的非常好的教程:http://developer.android.com/training/basics/data-storage/shared-preferences.html
试试下面的代码:
public class HelperActivity extends Activity {
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// start DataActivity because its your app first run
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
startActivity(new Intent(HelperActivity.this , DataActivity.class));
finish();
}
else {
startActivity(new Intent(HelperActivity.this , MainActivity.class));
finish();
}
}
}
此 activity 将帮助您确定它是否是第一个 运行 应用程序,并通过在 sharedPreference
中存储布尔值来根据它启动相应的 activity
将此activity设为您的启动器activity。
我有一个名为 DataActivity 的 activity,我希望它在我第一次打开我的应用程序时出现,以后再也不会出现。我希望我的 MainActivity 成为我的第一个 Activity,谁能告诉我该怎么做? (我对编码还很陌生,所以我将不胜感激......)谢谢! 亲切的问候! :)
您可以在 SharedPreferences
中保存一个条目,表明 DataActivity
已显示一次,然后在 DataActivity
onCreate()
中您可以执行以下操作:
boolean dataShownOnce = checkSharedPreferencesForDataActivityShown();
if (dataShownOnce) {
// Go to MainActivity
startActivity(new Intent(this, MainActivity.class);
finish();
} else {
// Showing DataActivity for the first time, continue with the normal logic
}
这里有一个关于使用 SharedPreferences
的非常好的教程:http://developer.android.com/training/basics/data-storage/shared-preferences.html
试试下面的代码:
public class HelperActivity extends Activity {
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// start DataActivity because its your app first run
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
startActivity(new Intent(HelperActivity.this , DataActivity.class));
finish();
}
else {
startActivity(new Intent(HelperActivity.this , MainActivity.class));
finish();
}
}
}
此 activity 将帮助您确定它是否是第一个 运行 应用程序,并通过在 sharedPreference
中存储布尔值来根据它启动相应的 activity将此activity设为您的启动器activity。