Return to a previous acitivity without lose data
-
29-06-2021 - |
Question
I have two activitys. Activity A e Activity B.
I have editText's with data inside Activity A. I pass to activity B.
But when i try to pass activity B to A, activity A restart and I lose the data in edittext. How I can pass of B to A with data inside A?
Here is my code in activity B. But doesn't work
Intent afectarQuotaSocios = new Intent(B.this,A.class);
afectarQuotaSocios.putExtra("ArrayListIdSocios",al);
afectarQuotaSocios.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(afectarQuotaSocios);
Any ideas? Thanks for your help
Solution
You should use
startActivityForResult(Intent intent, int requestCode);
to start a subactivity. Like in case of yours, its B.
To Return result from an activity to its super activity, call
setResult();
method
and get returned result in superactivity, in callback method, onActivityResult()
OTHER TIPS
You have to use the startActivityForResult method
Then on your B Activity you can pass your result :
resultIntent = new Intent(null);
resultIntent.putExtra(YOUR_CONSTANT_TEXT_IDENTIFIER, textValue);
setResult(Activity.RESULT_OK, resultIntent);
finish();
And you get the value on your A Activity on the onActivityResult method
You are starting a new Activity, which will call the started Activity's onCreate
method, and start over again. What you want to do in order to keep the same state, is to call finish()
from your Activity B.
Take a look at startActivityForResult
in order to get this behavior that you're seeking.
In the Manifest file simply add this Tag to the Activity A android:launchMode="singleInstance"
the onCreate won't be called for the Activity A and thus your data wouldn't be refreshed.
you create a new intent object,pass it back through the setResult(…) method call. After that the finish() method is called to give the control back to the parent activity.
Intent returnIntent = new Intent();
returnIntent.putExtra("SELECTVALUE",book);
setResult(RESULT_OK,returnIntent);
finish();
...
then you Back to your previous activity .
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case BOOK_SELECT:
if (resultCode == RESULT_OK) {
String name = data.getStringExtra("SELECTVALUE");
System.out.println("Name"+name);
}
} }