Intent data not clearing when user presses back bu

2019-04-28 23:01发布

I am puzzled over this behavior I am experiencing in an application I am developing...

Short:

Intent data is not clearing out when the user presses the back button to leave the application and then presses the recent button to re-enter the application. (Every other case, the intent data is cleared out)

Long:

I have an application with a splash screen that is used to collect data that is passed in from a URI scheme. I then setup an intent to forward the data to the main activity. The main activity has fragments and is based off the master/detail template.

The intent data is cleared out in all cases, such as pressing the home button and then going back to the application, pressing the recent apps button and then going back to the application, etc. The only case where the intent data is not cleared out is when the user presses the back button and then the recent apps button to get back into the application.

Relevant snippets of code that involve the intents:

// Splash Screen Activity
@Override
protected void onPostExecute(Void result) {
    // Data is done downloading, pass notice and app ids to next activity
    Intent intent = new Intent(getBaseContext(), ListActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("id1", id1);
    intent.putExtra("id2", id2);
    intent.putExtra("id3", id3);
    startActivity(intent);
    finish();
}


// ListActivity retrieving intent data
Intent intent = getIntent();
if (intent != null) {
    this.id1 = intent.getExtras().getString("id1");
    this.id2 = intent.getExtras().getString("id2");
    this.id3 = intent.getExtras().getString("id3");
}


// ListActivity clearing intent data
@Override
public void onPause() {
    super.onPause();
    // Clear intent data
    Intent intent = getIntent();
    intent.putExtra("id1", "");
    intent.putExtra("id2", "");
    intent.putExtra("id3", "");
}

I want to note that I have also tried using intent.removeExtra("id1") but that too did not work.

Any idea what is going on? It is as if Android is keeping the old intent even though onPause() is always called to clear the intent data.

4条回答
孤傲高冷的网名
2楼-- · 2019-04-28 23:21

To get around the issue I was facing, I opted to use SharedPreferences as a means to pass data between activities.

I know SharedPreferences isn't typically used for this purpose, but it solved my issue and works.

查看更多
3楼-- · 2019-04-28 23:29

I believe the difference here is that the Back key is actually causing your Activity to finish, where as pressing Home causes your activity to be paused, but not finished.
So when your process is brought back to the front in the Home case, it is simply resuming an already existing Activity instance, whereas in the Back case, the system is instantiating a new copy of your Activity, calling onCreate(), and handing it a fresh copy of the last Intent recorded for that activity.

查看更多
forever°为你锁心
4楼-- · 2019-04-28 23:33

actually this is due to Android starting the app from the history hence the intent extras are still in there

refer to this questions Android: Starting app from 'recent applications' starts it with the last set of extras used in an intent

so adding this conditional to handle this special case fixed it for me

int flags = getActivity().getIntent().getFlags();       
if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
    // The activity was launched from history
    // remove extras here
}
查看更多
啃猪蹄的小仙女
5楼-- · 2019-04-28 23:35

In onPause() you are clearing the extras in a "copy" of the Intent. You can try adding

setIntent(intent);

to onPause() after you've cleared the extras (although calling removeExtra() would probably also work instead of setting extras to empty strings).

NOTE: However, I would suggest that this design is flawed. You shouldn't use the Intent to keep track of state in your application. You should save some state in shared preferences because this will survive your app being killed/restarted, a reboot of the phone, or whatever.

The problem is that the new Intent is not persisted, so that If the user presses the HOME button and your app goes to the background and then Android kills your app because it is not active, when the user returns to the app, Android will create a new process for your app and it will recreate the activity using the original Intent.

Also, if you read the documentation for getIntent() it says that it returns the Intent that started the activity.

查看更多
登录 后发表回答