Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity Intent remains after shutdown

SETUP One Activity, SingleTop, receives an intent from a notification. Intent is consumed by activity. User hits back button to end the activity. onDestory gets called and isFinishing() returns true. Long press Home key to bring up recent apps. Launch previously closed application.

Similar situation occurs with onNewIntent when onStop is called after user presses home key on activity.

Problem Upon recreation of the activity after it's finished, the same intent from the notification is used. I don't want this. Is there a way to tell the system that we already consumed that notification so stop giving it to the activity? Work around? Suggestions?

What I have Tried

  1. I tried to remove extras from the intent. Not working. Clearning Activity's intent data randomly comes back, why?
  2. I tried to saveInstanceState() to keep a time stamp on intents. However onIstanceState(Bundle) is removed (null in onCreate) when activity is destroyed.
  3. I tried to set intent to null in activity but that does not work

This question is very similar to this one: http://grokbase.com/t/gg/android-developers/119jyzf492/getintent-removeextra-in-activity-doesnt-work-for-android-launchmode-“singletask”

like image 393
LEO Avatar asked Jul 28 '12 17:07

LEO


1 Answers

This is a WORK AROUND - and not a solution IMO.

The Problem

Remember the problem was onCreate() and onNewIntent() kept giving the activity the same intent no matter what (Nothing was sticky). The worst offender was onCreate() because the Bundle savedInstanceState was always null.

Work Around

I created a serializable (lets call is sFOO) class that holds intent actions and a time stamp from the intent extras. During onCreate() I load this serialized class (sFOO). During onStart() i compare sFOO to the intent being processed. If everything is new, i know the intent needs to be handled and sFoo updated then saved. If not, I disregard the intent.

Do You Need 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*/
            }

        }
    }
like image 156
LEO Avatar answered Oct 20 '22 07:10

LEO