Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permanently modify Intent that started an Activity

I would like send an Intent to start an Activity. I would like to be able to modify that Intent. Then, when the activity is destroyed and recreated, I would like those modifications to still be present when I call getIntent().

Currently, modifying the intent works fine as long as the Activity has not been destroyed. If it has, then when the activity is recreated, it will get the original Intent that started it, and not the copy it received when it was launched the first time that may have modified.

like image 254
Sky Kelsey Avatar asked Dec 07 '13 22:12

Sky Kelsey


People also ask

What type of Intent is used when you start an activity?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends.

How do I pass an Intent to another activity?

For this, Intent will start and the following methods will run: putExtra() method is used for sending the data, data in key-value pair key is variable name and value can be Int, String, Float, etc. getStringExtra() method is for getting the data(key) that is sent by the above method.

How do you start an activity using implicit Intent?

Syntax: ComponentType object = (ComponentType) findViewById(R. id. IdOfTheComponent); Intent i = new Intent(getApplicationContext(), <className>); startActivity(i);

How can I transfer data from one activity to another activity without Intent?

This example demonstrate about How to send data from one activity to another in Android without intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

Try that

activity.setIntent(activity.getIntent());

Works for me

like image 87
Igloob Avatar answered Oct 19 '22 18:10

Igloob


This is not the right thing to do it. What you want to do is save data right ? In such a case you don't have to mess with intents, just change the values and then save them, the next time the app runs it will load the values from last time, here is some code:

How to save values:

//Create sharedPref (It's android's 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);

More info here: developer.android.com/training/basics/data-storage/shared-preferences.html

like image 26
TomTsagk Avatar answered Oct 19 '22 19:10

TomTsagk