我想创建一个仅在应用程序启动后显示一次的屏幕。之后,它将只显示主屏幕。我实现这一点的方式只是检查首选项并根据标志设置当前布局。以这种方式实施它有什么缺点吗?有没有更好的办法?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here is the main layout
setContentView(R.layout.main);
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// second argument is the default to use if the preference can't be found
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
if (!welcomeScreenShown) {
//Here I set the one-time layout
setContentView(R.layout.popup_message);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit(); // Very important to save the preference
}
}