我有一个设置活动,我只想在第一次设置时显示。
然后我有一个加载闪屏,在5秒后重定向。
但是,当我保存我的设置活动时,它会进入启动页面,但启动页面会卡住并且不会重定向。
在设置被看到之后的每一个其他启动它一切正常。 它只是在第一次启动它是错误的。
有什么帮助吗?
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainactivity); // get shared preferences SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); // first time run? if (pref.getBoolean("firstTimeRun", true)) { // start the preferences activity startActivity(new Intent(this, Settings.class)); //get the preferences editor SharedPreferences.Editor editor = pref.edit(); // avoid for next run editor.putBoolean("firstTimeRun", false); editor.commit(); } else { new Handler().postDelayed(new Runnable() { @Override public void run() { final Intent mainIntent = new Intent(MainActivity.this, MainMenu.class); startActivity(mainIntent); finish(); } }, 5000); } } }I have a settings activity i want to show on first time setup only.
Then i have a loading splash screen which redirects after 5 seconds.
However when ive saved my settings activity, it goes to the splash page, but the splash page just gets stuck and doesnt redirect.
On every other startup after the settings have been seen it all works fine. Its just on the first time startup it is buggy.
Any help please?
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainactivity); // get shared preferences SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); // first time run? if (pref.getBoolean("firstTimeRun", true)) { // start the preferences activity startActivity(new Intent(this, Settings.class)); //get the preferences editor SharedPreferences.Editor editor = pref.edit(); // avoid for next run editor.putBoolean("firstTimeRun", false); editor.commit(); } else { new Handler().postDelayed(new Runnable() { @Override public void run() { final Intent mainIntent = new Intent(MainActivity.this, MainMenu.class); startActivity(mainIntent); finish(); } }, 5000); } } }最满意答案
这是因为Activity的生命周期。 从设置活动返回时,不会再次调用onCreate。 要解决此问题,您只需覆盖onResume()并移动即可
// get shared preferences SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); // first time run? if (pref.getBoolean("firstTimeRun", true)) { // start the preferences activity startActivity(new Intent(this, Settings.class)); //get the preferences editor SharedPreferences.Editor editor = pref.edit(); // avoid for next run editor.putBoolean("firstTimeRun", false); editor.commit(); } else { new Handler().postDelayed(new Runnable() { @Override public void run() { final Intent mainIntent = new Intent(MainActivity.this, MainMenu.class); startActivity(mainIntent); finish(); } }, 5000); }在里面(从onCreate中删除整个东西)
it's because of the Activity's lifecycle. When you came back from the settings activity, onCreate is not called again. To fix you can simply override onResume() and move
// get shared preferences SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); // first time run? if (pref.getBoolean("firstTimeRun", true)) { // start the preferences activity startActivity(new Intent(this, Settings.class)); //get the preferences editor SharedPreferences.Editor editor = pref.edit(); // avoid for next run editor.putBoolean("firstTimeRun", false); editor.commit(); } else { new Handler().postDelayed(new Runnable() { @Override public void run() { final Intent mainIntent = new Intent(MainActivity.this, MainMenu.class); startActivity(mainIntent); finish(); } }, 5000); }inside it (removing the whole thing from onCreate)
更多推荐
发布评论