Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
828 views
in Technique[技术] by (71.8m points)

java - How to show splash screen only when the app starts "fresh"?

I want to show the splash screen only once during the Application life cycle. Here is my code:

SplashScreenActivity.java:

final int welcomeScreenDisplay = 3000;

Thread welcomeThread = new Thread() {

    int wait = 0;

    @Override
    public void run() {
        try {
            super.run();

            while (wait < welcomeScreenDisplay) {
                sleep(1000);
                wait += 1000;
            }
        } catch (Exception e) {
            System.out.println("EXc=" + e);
        } finally {

            // Start other Activity
            startActivity(new Intent(SplashScreenActivity.this,
                    MainActiviey.class));
            finish();
        }
    }
};
welcomeThread.start();

Manifest:

    <activity android:name=".SplashScreenActivity" android:label="test"
    android:noHistory="true"
        android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden|keyboard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActiviey" android:label="test"
         android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden|keyboard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

The problem is if I press the hardware HOME botton to hide the app and open the App again at the application list. It will show the splash screen again (instead of showing the MainActivity). Is it possible to show splash screen only when the app starts "fresh" (not show at onresume() )? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't have this intent for two activities:

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

Also you might want to hide the splash screen from the history stack in case you decided launch another activity inside it as per Ash suggestion.

You can use this flag on your activity tag:

android:noHistory="true"  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...