Displays the splash screen only when launching the application from the launcher… here is a solution to the problem.
Displays the splash screen only when launching the application from the launcher
My SplashActivity
looks 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();
}
}
It is declared in AndroidManifext.xml
as follows:
<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 use WebView in my application. The problem is that whenever a user clicks on a link to my site in another activity in my app, it restarts from SplashActivity
. This is true to some extent, because that’s where I added the intent. However, I’m wondering if there is any other way this setting shows a splash screen only when the app is launched from the launcher or URL intent (when the app is not running), but not when the app is already running in the foreground when the URL intent is already running.
If this setup doesn’t work, what other way can I fix this?
Solution
Check if appLinkData is null and display SplashScreen otherwise, 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);
}
}
}