Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiple parent activities for using android back button

Tags:

I have an application that calls an activity several times from different activitys. So, im trying to implement the "back button" in the action bar for this activity. For doing this im using:

 switch (item.getItemId()) {     case android.R.id.home:         NavUtils.navigateUpFromSameTask(this);         return true;     default:                  return super.onOptionsItemSelected(item);     } 

and:

<meta-data             android:name="android.support.PARENT_ACTIVITY"             android:value="view.TweetsIndividuoActivity" /> 

The problem now, is that i cannt set a parent activity to my android manifest, cause, i don't know who is the parent of this activity.

What is the solution ?

Thanks

like image 263
Redes ConnectedGroup Avatar asked May 05 '14 15:05

Redes ConnectedGroup


People also ask

How to set parent activity in Android?

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.

How to manage activity stack in Android?

There is several way to remove a activity from the stack or prevent it to be stacked : To remove your activity from the stack , simply call finish() , see here. You can also implement in your manifest the property : android:noHistory="true" which prevent an activity to be stacked.

What happens to the back stack when you switch between activities?

If the user presses or gestures Back, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state.

What is Android parentActivityName?

As per docs -> section android:parentActivityName : The system reads this attribute to determine which activity should be started when the user presses the Up button in the action bar. The system can also use this information to synthesize a back stack of activities with TaskStackBuilder .


1 Answers

It's easier than you think.

switch (item.getItemId()) {     case android.R.id.home:         finish();         return true;     default:                  return super.onOptionsItemSelected(item); } 

Method finish() will destroy your activity and show the one that started it. That's what you want if I understood you right.

Your current solution is meant for cases when you want go back to the same parent every time e.g. Gmail app does it. When you open email from notification and then press actionbar back button it will not navigate back to HOME screen but it will show you Gmail inbox.

like image 126
Damian Petla Avatar answered Sep 28 '22 10:09

Damian Petla