Search code examples
androidhandlersplash-screen

Splash Screen Activity


I added splash screen to my application and my code looks as follow:

public class SplashActivity extends AppCompatActivity {

    public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds
    private Handler handler = null;

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

        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class);
                startActivity(intent);
                finish();
            }
        }, DELAY_MILLIS);
    }

    @Override
    protected void onStop() {
        super.onStop();
        handler.removeCallbacksAndMessages(null);
    }
}

My Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.vdovin.currencyratesapp">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:name=".application.CurrencyApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".screens.splash.SplashActivity"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity android:name=".screens.main.CurrencyExchangeActivity">
        </activity>
    </application>
</manifest>

So I faced with next problem:

If I hide my app with home button when loading splash screen, then when I open app again, splash screen activity not call CurrencyExchangeActivity. I understand that it appears because method onCreate() invoked only once, but I can't put it in onResume() because when I open my app again it shows me the splash screen again. But I want to show CurrencyActivity, like google's apps(maps, sheets etc...)


Solution

  • You cannot do that simply because you're setting the splash screen image as background of theme of your Activity. That's window level background which would show up regardless of your navigation or anything.

    If you really want to show splash screen only once in app lifetime then you'll have to do it in not the right way and have that image as part of your layout and inflate that layout using setContentView. Now when you come again in the app, you will call your CurrencyExchangeActivity even before setContentView of splash activity and which will just show a black window background and directly show up CurrencyExchangeActivity.

    Let me know if this makes sense or not. I can elaborate more if needed