Android activity task remains after shutdown

SETUP One action, SingleTop, receives the intent from the notification. Intention is absorbed by activity. Click the back button to complete the action. onDestory is called and isFinishing () returns true. Press the "Home" button to open the latest applications. Launch a previously closed application.

A similar situation occurs with onNewIntent when onStop is called after the user clicks on the home key for activity.

Problem . When activity is restored after its completion, the same intent from the notification is used. I do not want it. Is there any way to tell the system that we are already consuming this notification, so stop giving it this activity? Work around? Suggestions?

What i tried

  • I tried to remove add-ons from intent. Does not work. Clearning Activity intent data is accidentally returned, why?
  • I tried saving InstanceState () in order to keep timestamp intent. However, onIstanceState (Bundle) is deleted (null in onCreate) when activity is destroyed.
  • I tried to set the intent to null in action, but this does not work.

This question is very similar to this one: http://grokbase.com/t/gg/android-developers/119jyzf492/getintent-removeextra- in hyperactivity-doesnโ€™t work-for-android-launchmode- "singletask"

+6
source share
3 answers

This is WORK AROUND - not an IMO solution.

Problem

Remember that the problem was onCreate () and onNewIntent (), which gave the activity the same intention no matter what (nothing was sticky). The worst offender was onCreate (), because the Bundle savedInstanceState was always null.

Work around

I created a serializable (allows sFOO call) class that contains intentions and a timestamp from additional intentions. During onCreate (), I load this serialized class (sFOO). During onStart (), I compare sFOO with the processed intent. If everything is new, I know that I need to process the intent and update sFoo, and then save. If not, I ignore the intention.

Do you need a code?

In onStart ()

Intent activityIntent = this.getIntent(); if (activityIntent != null) { if (activityIntent.getAction() != null) { boolean newIntent = false; //Is the intent action being processes same as previous? if (activityIntent.getAction().compareTo(this.mLastProcessedIntent.mLastIntentProcessedAction) == 0) { if (activityIntent.getExtras() != null) { //Is the intent time stamp being processed same as previous? if (activityIntent.getExtras().getLong(TIME_STAMP_KEY) != this.mLastProcessedIntent.mLastIntentProcessedTimestamp) { newIntent = true; } } } else { Log.d(TAG,"Last processed intent action does not equal new one."); newIntent = true; } if (newIntent) { updateAndSaveProcessedIntent(); /*YOUR CODE HERE TO HANDLE INTENT*/ } } } 
+4
source

It looks like the intention you use to start your activity could be conveyed as a โ€œstickyโ€ intention. Sticky intentions make intention available even after its consumption. If so, your application will receive the same intention as before. If you can check everything that translates this intention to see if it is a sticky intention or not. You will see this in the manifest:

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

and it will be sent using this method:

 sendStickyBroadcast(intent); 

If you have no way to change this (unless it is the application invoking it), you will most likely have to find a way to deal with it that is suitable for your application. This is only one possibility without seeing any code.

0
source

My solution was to first change my activity to android:launchMode="singleTop" Then use onNewIntent

@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); }

0
source

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


All Articles