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.
To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends.
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.
Syntax: ComponentType object = (ComponentType) findViewById(R. id. IdOfTheComponent); Intent i = new Intent(getApplicationContext(), <className>); startActivity(i);
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.
Try that
activity.setIntent(activity.getIntent());
Works for me
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With