简体   繁体   English

首次设置后,Android Splash活动未重定向

[英]Android Splash activity not redirecting after first time setup

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. 然后,我有一个加载启动屏幕,该屏幕在5秒后重定向。

However when ive saved my settings activity, it goes to the splash page, but the splash page just gets stuck and doesnt redirect. 但是,当ive保存我的设置活动时,它会转到初始页面,但初始页面只会卡住并且不会重定向。

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);
        }
    }
}

it's because of the Activity's lifecycle. 这是因为该活动的生命周期。 When you came back from the settings activity, onCreate is not called again. 当您从设置活动回来时,不会再次调用onCreate。 To fix you can simply override onResume() and move 要解决此问题,您只需覆盖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);
        }

inside it (removing the whole thing from onCreate) 在里面(从onCreate中删除整个内容)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM