Show splash screen only when launching the app from launcher

Hrishikesh Kokate :

I have my SplashActivity like this:

public class SplashActivity extends Activity {

    Handler Handler;

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

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

        Intent appLinkIntent = getIntent();
        String appLinkAction = appLinkIntent.getAction();
        Uri appLinkData = appLinkIntent.getData();
    }
}

And its declaration in AndroidManifext.xml like this:

<activity
    android:name=".SplashActivity"
    android:theme="@style/Splash"
    android:launchMode="singleTop">

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

    <meta-data
        android:name="android.app.shortcuts"
        android:resource="@xml/shortcuts"/>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="brokenhearts.ml"
            android:scheme="http"
            android:pathPattern="/*"/>
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="brokenhearts.ml"
            android:scheme="https"
            android:pathPattern="/*"/>
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="www.brokenhearts.ml"
            android:scheme="http"
            android:pathPattern="/*"/>
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="www.brokenhearts.ml"
            android:scheme="https"
            android:pathPattern="/*"/>
    </intent-filter>
</activity>

I'm using WebViews in my app. The problem is, whenever a user taps on a link of my website in some other activity of my app, it starts from SplashActivity all over again. That's in a way correct, as that's where I have added the intents. However, I'd like to know if there is any other way with this setup to show the splash screen only when the app is launched from the launcher or from the URL intent (when the app is not running) and not when the URL intent is triggered when the app is already running in the foreground.

If it's not possible with this set-up, what's the other way I can go about this?

MJM :

Check if appLinkData is null then display SplashScreen else start MainScreen

public class SplashActivity extends Activity
{
    Handler Handler;

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

         Intent appLinkIntent = getIntent();
        String appLinkAction = appLinkIntent.getAction();
        Uri appLinkData;
        if(appLinkAction!=null)
        appLinkData = appLinkIntent.getData();

        if(appLinkData!=null)
        {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
        finish();
         return;
        }else
        {

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

       }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=94254&siteId=1