27

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);
        }
    }
}
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Steven Schoen
  • 4,366
  • 5
  • 34
  • 65

7 Answers7

38

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 .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • What if its a fragment ? how to do it ? – user2056563 Mar 19 '15 at 08:27
  • @user2056563 similar way. You use "getActivity" for when you need the activity, and also "setHasOptionsMenu(true)" inside the "onCreate" of the fragment. don't forget to call the "super.onCreateOptionsMenu" for the fragment's "onCreateOptionsMenu" method (I call it at the end, but I think it's ok at the beginning too). – android developer Mar 19 '15 at 15:14
  • `Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the android:parentActivityName attribute in the element.` taken from http://developer.android.com/training/implementing-navigation/ancestral.html , So I would correct that last bit of info – Bhargav Mar 31 '16 at 05:16
  • @Bhargav What is there to correct? If you use the support library, you use the meta-data tag I've mentioned – android developer Mar 31 '16 at 05:29
  • @androiddeveloper `if the API is 11 or above, you can add an attribute of parentActivityName with the path to the main activity instead of the metadata .` – Bhargav Mar 31 '16 at 05:30
  • 1
    @Bhargav Oh, you mean it's API 16 and not 11 . ok i will fix this. thanks. – android developer Mar 31 '16 at 06:32
  • Kotlin: intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP) – Morgz Aug 03 '17 at 16:21
  • @Morgz Seems to be the same to me – android developer Aug 06 '17 at 11:29
14

Found out the root of my problem was a change in the manifest I made a while ago: I added this line:

android:launchMode="singleInstance"

so my main activity wouldn't be relaunched. Changing it to:

android:launchMode="singleTask"

solved my problems, and I was able to remove all the onOptionsItemSelected stuff. I knew there was something wrong, that Android should have been able to handle this better, and I was right. Check the manifest :P

Regexident
  • 29,441
  • 10
  • 93
  • 100
Steven Schoen
  • 4,366
  • 5
  • 34
  • 65
  • so using the code you've published would empty all of the activities besides the main activity (which is also the first one) ? – android developer Jun 10 '13 at 09:34
  • Android documentation says "The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications." http://developer.android.com/guide/topics/manifest/activity-element.html – user3533716 Apr 23 '16 at 10:49
12

You can Handle Action Bar button by using Code or XML.

Check this code

if you want it programmatically Add this line in onCreate() method

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

And Override this method onSupportNavigateUp()

@Override
public boolean onSupportNavigateUp(){  
finish();  
return true;  
}

OR Non-programmatically you can add meta to manifest file as

<activity android:name="Current Activity"
        android:parentActivityName="Activity you want to open">
    </activity>

Note: Make sure Actionbar is not null.

Naeem Ibrahim
  • 3,375
  • 1
  • 21
  • 21
10

In your onCreate(Bundle savedInstanceState), do

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

Then in your onOptionsItemSelected(MenuItem item), do

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // go to previous screen when app icon in action bar is clicked
            Intent intent = new Intent(this, PreviousActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
    }
    return super.onOptionsItemSelected(item);
}
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • 3
    This is exactly what I have. It's not functioning like Google's Up button. It plays the animation for launching a new activity (and not returning to the last one). – Steven Schoen Jul 03 '12 at 04:39
  • ... well this worked fine for me. maybe you should post your code? Are you importing the correct `MenuItem`? Are you extending `SherlockActivity`? I don't know what else to suggest haha – Alex Lockwood Jul 03 '12 at 04:42
  • @D_Steve595 I am having the same problem. Did you ever figure out how to make the animation the same as the normal back button? – theblang Mar 13 '14 at 15:36
5

For who want to just go back in the previous activity, this is the simplest solution:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish(); //this method close current activity and return to previous
            return true;
    }
    return super.onOptionsItemSelected(item);
}
minux
  • 2,694
  • 2
  • 18
  • 16
0
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings2);
    //here is to put the up back button in the Activity
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
// here is the code to handle to go back to the activity which sent you to this current one
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}
-1

make sure your android:minSdkVersion="11" which could be seen in the manifest file, Up icon has been included from APK 11. I have made a small sample plz try the below link which may be help you just import into your work space

http://www.mediafire.com/?hktdvts7yyduwv1

Rehan
  • 14
  • 3
  • The `up icon` can be used `before Api Level 11` with the `Google support`. You would instead call `getSupportActionBar().setDisplayHomeAsUpEnabled(true)`. – theblang Mar 13 '14 at 14:30