Intent data = new Intent(); data. putExtra("myData1", "Data 1 value"); data. putExtra("myData2", "Data 2 value"); // Activity finished ok, return the data setResult(RESULT_OK, data); finish();
Declare a Parent Activity You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.
You declared activity A with the standard launchMode
in the Android manifest. According to the documentation, that means the following:
The system always creates a new instance of the activity in the target task and routes the intent to it.
Therefore, the system is forced to recreate activity A (i.e. calling onCreate
) even if the task stack is handled correctly.
To fix this problem you need to change the manifest, adding the following attribute to the A activity declaration:
android:launchMode="singleTop"
Note: calling finish()
(as suggested as solution before) works only when you are completely sure that the activity B instance you are terminating lives on top of an instance of activity A. In more complex workflows (for instance, launching activity B from a notification) this might not be the case and you have to correctly launch activity A from B.
Updated Answer: Up Navigation Design
You have to declare which activity is the appropriate parent for each activity. Doing so allows the system to facilitate navigation patterns such as Up because the system can determine the logical parent activity from the manifest file.
So for that you have to declare your parent Activity in tag Activity with attribute
android:parentActivityName
Like,
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.app_name.A" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name=".B"
android:label="B"
android:parentActivityName="com.example.app_name.A" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app_name.A" />
</activity>
With the parent activity declared this way, you can navigate Up to the appropriate parent like below,
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
So When you call NavUtils.navigateUpFromSameTask(this);
this method, it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward as defined by FLAG_ACTIVITY_CLEAR_TOP
.
And to display Up button you have to declare setDisplayHomeAsUpEnabled():
@Override
public void onCreate(Bundle savedInstanceState) {
...
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Old Answer: (Without Up Navigation, default Back Navigation)
It happen only if you are starting Activity A again from Activity B.
Using startActivity()
.
Instead of this from Activity A start Activity B using startActivityForResult()
and override onActivtyResult()
in Activity A.
Now in Activity B just call finish()
on button Up. So now you directed to Activity A's onActivityResult()
without creating of Activity A again..
I had pretty much the same setup leading to the same unwanted behaviour. For me this worked:
adding the following attribute to an activity A in the Manifest.xml
of my app:
android:launchMode="singleTask"
See this article for more explanation.
Although an old question, here is another (imho the cleanest and best) solution as all the previous answeres didn't work for me since I deeplinked Activity B from a Widget.
public void navigateUp() {
final Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) {
Log.v(logTag, "Recreate back stack");
TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
} else {
NavUtils.navigateUpTo(this, upIntent);
}
}
[https://stackoverflow.com/a/31350642/570168 ]
But also see: https://speakerdeck.com/jgilfelt/this-way-up-implementing-effective-navigation-on-android
A better way to achieve this is by using two things: call:
NavUtils.navigateUpFromSameTask(this);
Now, in order for this to work, you need to have your manifest file state that activity A has a parent activity B. The parent activity doesn't need anything. In version 4 and above you will get a nice back arrow with no additional effort (this can be done on lower versions as well with a little code, I'll put it below) You can set this data in the manifest->application tab in the GUI (scroll down to the parent activity name, and put it by hand)
Support node:
if you wish to support version below version 4, you need to include metadata as well. right click on the activity, add->meta data, name =android.support.PARENT_ACTIVITY and value = your.full.activity.name
to get the nice arrow in lower versions as well:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
please note you will need support library version 7 to get this all working, but it is well worth it!
What worked for me was adding:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
finish();
}
to TheRelevantActivity.java
and now it is working as expected
and yeah don't forget to add:
getSupportActionbar.setDisplayHomeAsUpEnabled(true);
in onCreate()
method
I tried android:launchMode="singleTask"
, but it didn't help.
Worked for me using android:launchMode="singleInstance"
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