Android Splash活动在首次设置后未重定向(Android Splash activity not redirecting after first time setup)

编程入门 行业动态 更新时间:2024-10-08 06:22:04
Android Splash活动在首次设置后未重定向(Android Splash activity not redirecting after first time setup)

我有一个设置活动,我只想在第一次设置时显示。

然后我有一个加载闪屏,在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)

更多推荐

本文发布于:2023-07-27 08:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287703.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:首次   重定向   Splash   Android   setup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!