Android Activity Intent remains after shutdown

天大地大妈咪最大 提交于 2019-12-04 07:18:15

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

        }
    }

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!