Android Splash activity 首次设置后未重定向

Android Splash activity not redirecting after first time setup

我有一个设置activity我只想在第一次设置时显示。

然后我有一个加载启动画面,它会在 5 秒后重定向。

然而,当我保存我的设置时 activity,它会转到启动页面,但启动页面只是卡住了并且没有重定向。

在看到设置后的每一次启动时,一切正常。它只是在第一次启动它是错误的。

有什么帮助吗?

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 的生命周期。当您从设置 activity 返回时,不会再次调用 onCreate。要修复你可以简单地覆盖 onResume() 和 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);
        }

在里面(从 onCreate 中删除整个东西)