Intent data is not cleared when the user clicks the back button and then the recent apps button

I am puzzled by this behavior that I experience in the application that I am developing ...

Short:

Intent data is not cleared when the user presses the back button to exit the application, and then presses the last button to re-enter the application. (In each other case, intent data is cleared)

Long

I have a screensaver application that is used to collect data that is passed from a URI scheme. Then I set the intent to redirect the data to the main action. The main action has fragments and is based on a master / part template.

Intent data is cleared in all cases, such as pressing the home button, then returning to the application, pressing the button for the latest applications, and then returning to the application, etc. The only case where the intent data is not cleared when the user clicks the back button and then the Recent Applications button to return to the application.

Relevant code fragments that include intent:

// 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 also tried to use aim.removeExtra ("id1"), but that didn't work either.

Any idea what is going on? This is similar to the fact that Android retains the previous intention, although onPause () is always called to clear the intent data.

+6
source share
4 answers

To get around the problem that I was facing, I decided to use SharedPreferences as a means to transfer data between actions.

I know that SharedPreferences is not usually used for this purpose, but it solved my problem and works.

+1
source

in fact, this is due to the fact that Android launches the application from history, therefore, functions are still not available

refer to these Android questions : launching an application from the “latest applications” launches it with the latest set of additional features used in intent

so adding this condition to handle this special case, he 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 } 
+21
source

I believe that the difference is that the Back key actually terminates your Activity when clicking on Home pauses the action, but is not completed.
So, when your process returns to the forefront of the Home case, it simply resumes the existing instance of Activity, while in the Back case, the system creates an instance of a new copy of your Activity, calling onCreate() and passing it a new copy of the last intent recorded for this action .

+2
source

In onPause() you clear the extra functions in the Intent copy . You can try adding

 setIntent(intent); 

to onPause() after you have cleared the extra functions (although calling removeExtra() will probably work instead of setting extra functions for empty strings).

Note: However, I would suggest that this design is erroneous. You should not use Intent to track status in your application. You have to save a certain state in general preferences, because it will sustain your application, which will be killed / restarted, reboot the phone or something else.

The problem is that the new Intent not saved, so if the user presses the HOME button and your application goes to the background, and then Android kills your application because it is inactive when the user returns to the application, Android will create a new process for your application , and he will recreate activity with the original intention .

In addition, if you read the documentation for getIntent() , it will report that it returns the intent that triggered the action .

+1
source

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


All Articles