Constantly change the intention that initiated the action

I would like to send an Intent to start the Activity. I would like to be able to change this intention. Then, when the action is destroyed and recreated, I would like these modifications to still be present when I call getIntent() .

Currently, a change in intent is working fine until the activity has been destroyed. If so, then when the activity is recreated, it will receive the original Intent, which launched it, and not a copy of it, when it was launched for the first time, when it can be changed.

+6
android android-intent
Dec 07 '13 at 22:46
source share
3 answers

Changing the intention to delete my additional data works fine while the main action still exists, but if it is destroyed / restored, the additional data will be returned.

This is because you are changing your local copy of Intent , and not the main copy, which is supported on the OS where task lists are stored.

If this data is the true state of the activity instance, it should be saved as such through onSaveInstanceState() , and you will get it back through onRestoreInstanceState() . Your library user will need to forward these events to you.

If you do not want to consider this as an instance state, but rather a process state, save the data in singleton mode.

If the data must live outside the lifetime of the process, write it to disk somewhere.

I could save the data in the receiver of the host broadcast application and then use and delete it in my code

If "save data to the broadcast receiver of the host application", it makes no sense. The registered receiver lives for one broadcast and then runs.

+9
Dec 07 '13 at 23:35
source share

It is not right. What do you want to do is save the data correctly? In this case, you don’t have to bother with the intentions, just change the values ​​and then save them, the next time the application starts, it will load the values ​​from the last time, here is the code:

How to save values:

 //Create sharedPref (It android way of saving values) SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); //Save Values here editor.putInt(getString(R.string.saved_high_score), newHighScore); //Commit changes editor.commit(); 

How to load values:

 SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); int defaultValue = getResources().getInteger(R.string.saved_high_score_default); 

Learn more here: developer.android.com/training/basics/data-storage/shared-preferences.html

+3
Dec 07 '13 at 22:54
source share

Try

 activity.setIntent(activity.getIntent()); 

Works for me

0
Jul 24 '16 at 20:43
source share



All Articles