我希望我的应用仅在首次启动时启动 Activity。 有人知道吗?
我找到了这个,但它只显示黑屏。
public class WhatsNew extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
protected void onCreate(Bundle state){
super.onCreate(state);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean dialogShown = settings.getBoolean("dialogShown", false);
if (!dialogShown) {
// AlertDialog code here
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("dialogShown", true);
editor.commit();
}
}
}
最佳答案
当应用程序启动时,在 Activity 首选项中设置一个标志,表明该 Activity 已经运行。将您的设置默认为 false,然后仅在未设置标志时才启动该 Activity 。请注意,如果用户清除您的应用程序数据,或将其卸载并稍后再次安装,该 Activity 将再次显示。
关于android - 只开始 Activity 一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7351045/