DR93
DR93

Reputation: 583

Terminate all other previous activities when splash activity starts

In my app I always want user to start from Splash screen. For example, my app may be open in background and some notification pops up which starts splash activity. This should terminate all previous activities which were running.

I have accomplished this by storing list of all running activities references. And when splash activity starts it just calls

for(runningActivity : runningActivitiesList) {
    runningActivity.finish();
}

This solution works well. However, Android Studio gives me warning of memory leaks when storing references to activities.

Can someone please suggest me a better approach which avoids memory leaks?

Upvotes: 5

Views: 690

Answers (4)

DR93
DR93

Reputation: 583

Tried all other options, but only thing worked for me is:

final Intent intent = new Intent(applicationContext, SplashActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
            | Intent.FLAG_ACTIVITY_NEW_TASK);
    return IntentCompat.makeRestartActivityTask(intent.getComponent());

Please NOTE: This solution is also not full proof. Since, when I open my app through Google Play Store it launches splash activity even when another instance of app is running in background. Thus I end up having 2 instances of the same activity.

Upvotes: 1

Juan
Juan

Reputation: 5589

In android manifest set:

android:launchMode="singleTop"

For notifications generated from your app you can use @mac229's flags in @Nischal's pending intent.

Upvotes: 0

Nischal
Nischal

Reputation: 880

You don't need to finish all running/previous applications.Instead you can start your activity using TaskBuilder api to handle proper back navigation.

Open your activity with this:

private static PendingIntent makePendingIntent(@NonNull Context context, @NonNull Intent resultIntent) {
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack
stackBuilder.addParentStack(YourActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}

In your manifest file define the parent activity of YourActivity.class as:

<activity
        android:name=".YourActivity"
        android:parentActivityName=".MainActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateAlwaysHidden|adjustPan">           
 </activity>

Follow these urls for more details: http://developer.android.com/reference/android/support/v4/app/TaskStackBuilder.html http://developer.android.com/guide/components/tasks-and-back-stack.html http://www.programcreek.com/java-api-examples/index.php?api=android.app.TaskStackBuilder

Upvotes: 0

mac229
mac229

Reputation: 4849

Maybe enough is to start Activity with clear stack:

Intent intent = new Intent(context, clazz);
intent.setFlags(IntentCompat.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Upvotes: 5

Related Questions