Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to handle action bar up button?

I use ActionBarSherlock (although I don't think it matters).

I have a Main activity and an About activity. I want the About activity to show the back-arrow by its logo, and do the proper animation and such. I don't know how to do this properly.

Currently, I have under onOptionsMenuItemSelected to launch the Main activity when the Up/Home button is pressed, but it's hacky and doesn't really work right. It plays the wrong animation, and handles multitasking poorly.

How do I set this up right?

Here's the part in my Main activity that launches the About:

Intent aboutIntent = new Intent(MainActivity.this, About.class); MainActivity.this.startActivity(aboutIntent); 

Here's my About activity:

package com.stevenschoen.test;  import android.content.Intent; import android.os.Bundle;  import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.MenuItem;  public class About extends SherlockActivity {     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.about);          getSupportActionBar().setDisplayHomeAsUpEnabled(true);         getSupportActionBar().setDisplayShowTitleEnabled(false);     }      public boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {              case android.R.id.home:                 // app icon in action bar clicked; go home                 Intent intentHome = new Intent(this, MainActivity.class);                 intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);                 startActivity(intentHome);                 return true;              default:                 return super.onOptionsItemSelected(item);         }     } } 
like image 622
Steven Schoen Avatar asked Jul 03 '12 04:07

Steven Schoen


People also ask

What is up button in android?

When the user selects the Up button, the app navigates to the parent activity. This lesson shows you how to add an Up button to an activity by declaring the activity's parent in the manifest, and enabling the app bar's Up button.

What does an action bar do?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

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 .

How do you add action items to the action bar in android?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


1 Answers

Have you also tried this (taken from Android website here) :

in the manifest, for each activity X that needs to go to the main activity, add this to the code:

@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); } 

and this to its manifest xml tag:

<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.activities.MainActivity" /> 

if you need to still have the same state on the main activity, use this code instead :

Intent intent = NavUtils.getParentActivityIntent(this);  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);  NavUtils.navigateUpTo(this, intent); 

if the API is 16 or above, you can add an attribute of parentActivityName with the path to the main activity instead of the metadata .

like image 176
android developer Avatar answered Sep 28 '22 08:09

android developer