Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to update the intent used to create an activity?
    text
    copied!<p>Is it possible to modify an intent used to create an activity from inside that activity itself.</p> <p><strong>Simple Description</strong> Is it possible to update the intent that is returned by </p> <pre><code>getIntent() </code></pre> <p>with new values and have it use those new values if an activity is recreated either due to the system killing it for resources, or orientation change, or when using developers options do not keep activities option is selected (which forces a recreate of an activity each time you leave it and come back to it).</p> <p><strong>What I'm trying to achieve</strong></p> <p>I am trying to achieve a way to pass variables(and update them) between activities without the use of a database or SharedPreferences. I want the data to only be alive when the app is, but also be easily recreatable by androids system prefs. It was just an idea I had, to try something new. I did not know that my explanation would be so hard to understand. Sorry about that.</p> <p><strong>Complex Description</strong></p> <p><strong>Scencario:</strong></p> <p>From MainActivity I create another Activity (ActivityA) with below code</p> <pre><code>Intent tmp = new Intent(getApplicationContext(), ActivityA.class); tmp.putExtra("Key", "Value"); startActivity(tmp); </code></pre> <p>Then in ActivityA print out that value</p> <pre><code>Log.i("TAG", getIntent().getExtras().getString("Key")); </code></pre> <p>the above line will print out Value in the log</p> <p>Then in ActivityA i will press a button to start ActivityB with below code</p> <pre><code>Intent tmp = newIntent(getApplicationContext(), ActivityB.class); startActvityForResult(tmp, 1000) //1000 is the result code </code></pre> <p>Then when ActivityB starts when I press a button I want to send back data to ActivityA with below code:</p> <pre><code>Intent returnIntent = new Intent(); returnIntent.putExtra("Test1", "Value2"); setResult(RESULT_OK, returnIntent); finish(); </code></pre> <p>All the above works with no problems. What I want to do is in ActivityA's onActivityResult(...) modify the intent used to create ActivityA with the value returned here</p> <pre><code>public void onActivityResult(int requestCode, int resultCode, Intent data) { //modify intent used to create this activity with value received in data } </code></pre> <p>So that when the activity gets recreated on orientation change OR (Developer option do not keep activity) when I leave the application the getIntent() function will return the new data (the one updated in onActivityResult(...)) and not the one that was used to originally create it (the call from MainActivity).</p> <p>Is such a thing possible? I have been messing around with</p> <pre><code>setIntent(getIntent().putExtra("Key", "value obtained Intent data")) </code></pre> <p>but when the activity is recreated after going home the getIntent brings the first intent (the one used to create it originally) and not the modified intent that was created in onActivityResult(...)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload