Complete all other previous steps when starting the spray

In my application, I always want the user to start from the Splash screen. For example, my application may be open in the background, and some kind of notification appears that triggers a burst of activity. This should terminate all previous actions that have been performed.

I accomplished this by keeping a list of all links to current actions. And when a surge of activity begins, it just causes

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

This solution works well. However, Android Studio warns about a memory leak when storing action links.

Can someone suggest me a better approach that avoids memory leaks?

+5
source share
4 answers

Tried all other options, but only for me it worked:

 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 decision is also not a complete proof. Because when I open my application through the Google Play Store, it triggers a burst of activity, even when another instance of the application is running in the background. This way I get 2 instances of the same action.

0
source

It might be enough to run Activity with an explicit stack:

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

You do not need to end all running / previous applications. Instead, you can start your activity with the TaskBuilder api for proper reverse navigation.

Open your activity as follows:

 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, specify the parent activity of YourActivity.class as:

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

Follow these addresses 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

0
source

In the Android manifest set:

Android: launchMode = "SingleTop"

For notifications created in your application, you can use the @ mac229 flags in the pending @Nischal intent.

0
source

Source: https://habr.com/ru/post/1271663/


All Articles