Android Activity Intent remains after shutdown
Asked Answered
G

3

6

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”

Gynecologist answered 28/7, 2012 at 17:38 Comment(0)
G
4

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*/
            }

        }
    }
Gynecologist answered 28/7, 2012 at 20:57 Comment(0)
S
0

It sounds like the Intent you are using to start your Activity might have been broadcast as a "sticky" Intent. Sticky Intents make the Intent available even after its been consumed. If that is the case your app will get the same Intent that was used before. If you can check whatever is broadcasting that Intent to see if it's a sticky Intent or not. You will see this in the manifest:

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

and it will be sent with this method:

sendStickyBroadcast(intent);

If you don't have the ability to change that (if its not your app calling it) then you likely will just have to find a way to deal with it that is appropriate for your app. This is just one possibility without seeing any code.

Schema answered 28/7, 2012 at 17:52 Comment(2)
'@Jason' The intent comes from a notification that my application started. When user clicks on notification it sends an intent to my application. I don't believe notifications send intents to activities using sticky. I implemented removeStickyBroadcast() with the proper permission and nothing changed. Problem still persists.Gynecologist
Broadcast Intents and Intents used to start Activitys are 2 completely different things. There is no such thing as a sticky Intent for launching an Activity. This answer is likely to confuse people, it should be removed.Rodolforodolph
C
0

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); }

Cortico answered 21/2, 2013 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.