启动画面不止一次

时间:2016-08-06 13:23:57

标签: android splash

启动画面应该只在安装应用程序时出现一次。

AndroidManifest

<activity android:name=".Splash"
        android:noHistory="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Splash.java

public class Splash extends AppCompatActivity {

private static boolean splashLoaded = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!splashLoaded) {
        setContentView(R.layout.splash);
        int secondsDelayed = 5;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                startActivity(new Intent(Splash.this, MainActivity.class));
                finish();
            }
        }, secondsDelayed * 500);

        splashLoaded = true;
    }
    else {
        Intent goToMainActivity = new Intent(Splash.this, MainActivity.class);
        goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(goToMainActivity);
        finish();
    }
}
}

但即使我放android:noHistory="true"后,我的工作也不起作用 还有什么我应该补充的吗?

3 个答案:

答案 0 :(得分:1)

public class Splash extends AppCompatActivity {

    private static boolean splashLoaded = false;
    SharedPreferences prefs;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefs = PreferenceManager.getDefaultSharedPreferences(this);

        splashLoaded = prefs.getBoolean("Splash_Loaded", false);

        if (!splashLoaded) {
            setContentView(R.layout.splash);
            int secondsDelayed = 5;
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    startActivity(new Intent(Splash.this, MainActivity.class));
                    finish();
                }
            }, secondsDelayed * 500);

            splashLoaded = true;
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("Splash_Loaded", true);
            editor.apply();
        } else {
            Intent goToMainActivity = new Intent(Splash.this, MainActivity.class);
                goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(goToMainActivity);
            finish();
        }
    }
}

答案 1 :(得分:0)

您可以扩展Application类以将变量存储在不同的活动之外。应用程序仅在应用程序启动时启动,每次打开特定窗口时都会启动活动。有关扩展Application类的更多信息,请参阅此链接:https://www.google.be/url?sa=t&source=web&rct=j&url=http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/&ved=0ahUKEwi3qIaD-KzOAhVCWxoKHURMDgcQFggdMAE&usg=AFQjCNF0ZhP1KDNAUnJNY0YyvZvrm_Vn8g

答案 2 :(得分:0)

使用此代码 -

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences sharedPreferences = getSharedPreferences("AnyName",MODE_PRIVATE);
    Boolean loaded = sharedPreferences.getBoolean("loaded",false);

    if (!loaded) {
        setContentView(R.layout.splash);
        SharedPreferences.Editor editor2 = sharedPreferences.edit();
        editor2.putBoolean("loaded", true);
        editor2.commit();
        int secondsDelayed = 5;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                startActivity(new Intent(Splash.this, MainActivity.class));
                finish();
            }
        }, secondsDelayed * 500);

        }
    else {
        Intent goToMainActivity = new Intent(Splash.this, MainActivity.class);
        goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(goToMainActivity);
        finish();
    }
}