mobile app Beginner
mobile app Beginner

Reputation: 1661

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!

Upvotes: 2

Views: 7571

Answers (3)

Harshit Jain
Harshit Jain

Reputation: 500

public class SplashActivity extends AppCompatActivity {

Handler handler;
private final int SPLASH_DISPLAY_LENGTH = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    SplashStart();

}

private void SplashStart() {
    handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, SPLASH_DISPLAY_LENGTH);
}


@Override
protected void onResume() {
    super.onResume();

    }
}

Upvotes: 0

Kalimah
Kalimah

Reputation: 11437

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"  

Upvotes: 6

Ash
Ash

Reputation: 1721

Yes, it's possible. Use SharedPreferences to store a flag, that would indicate that your splash has already been shown. Check it in onCreate() method of your splash screen and if it is present, launch the next activity.

Upvotes: 2

Related Questions