Add back button to action bar





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







62















I have been trying to add a back button to the action bar.



I want my view to look like this:
enter image description here



I want to add the back button in the left of the action bar.



I added this code



ActionBar actionBar = getActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);


but it doesn't work.



How can I fix this?










share|improve this question




















  • 5





    Up button it not them same with back button. And i think add back button is anti Android pattern

    – Nam Trung
    Aug 22 '12 at 10:18






  • 6





    The screenshot is of an iPhone. Android does not run on such devices. Android has its own back button, always available for the user; you do not need to put one in the action bar.

    – CommonsWare
    Aug 22 '12 at 12:39






  • 3





    Many apps today (2014) put a back/up button in the actionbar (eg. Instagram) so I even if this is an antipattern.. It is clearly a need in users (maybe users going from iPhone see more benefit on this)

    – sports
    Oct 6 '14 at 18:42











  • please check this answer

    – Amit Vaghela
    Sep 26 '16 at 11:58




















62















I have been trying to add a back button to the action bar.



I want my view to look like this:
enter image description here



I want to add the back button in the left of the action bar.



I added this code



ActionBar actionBar = getActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);


but it doesn't work.



How can I fix this?










share|improve this question




















  • 5





    Up button it not them same with back button. And i think add back button is anti Android pattern

    – Nam Trung
    Aug 22 '12 at 10:18






  • 6





    The screenshot is of an iPhone. Android does not run on such devices. Android has its own back button, always available for the user; you do not need to put one in the action bar.

    – CommonsWare
    Aug 22 '12 at 12:39






  • 3





    Many apps today (2014) put a back/up button in the actionbar (eg. Instagram) so I even if this is an antipattern.. It is clearly a need in users (maybe users going from iPhone see more benefit on this)

    – sports
    Oct 6 '14 at 18:42











  • please check this answer

    – Amit Vaghela
    Sep 26 '16 at 11:58
















62












62








62


18






I have been trying to add a back button to the action bar.



I want my view to look like this:
enter image description here



I want to add the back button in the left of the action bar.



I added this code



ActionBar actionBar = getActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);


but it doesn't work.



How can I fix this?










share|improve this question
















I have been trying to add a back button to the action bar.



I want my view to look like this:
enter image description here



I want to add the back button in the left of the action bar.



I added this code



ActionBar actionBar = getActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);


but it doesn't work.



How can I fix this?







android android-layout android-actionbar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 11 '18 at 12:31









Vadim Kotov

4,84863549




4,84863549










asked Aug 22 '12 at 10:03









haythem souissihaythem souissi

1,64754074




1,64754074








  • 5





    Up button it not them same with back button. And i think add back button is anti Android pattern

    – Nam Trung
    Aug 22 '12 at 10:18






  • 6





    The screenshot is of an iPhone. Android does not run on such devices. Android has its own back button, always available for the user; you do not need to put one in the action bar.

    – CommonsWare
    Aug 22 '12 at 12:39






  • 3





    Many apps today (2014) put a back/up button in the actionbar (eg. Instagram) so I even if this is an antipattern.. It is clearly a need in users (maybe users going from iPhone see more benefit on this)

    – sports
    Oct 6 '14 at 18:42











  • please check this answer

    – Amit Vaghela
    Sep 26 '16 at 11:58
















  • 5





    Up button it not them same with back button. And i think add back button is anti Android pattern

    – Nam Trung
    Aug 22 '12 at 10:18






  • 6





    The screenshot is of an iPhone. Android does not run on such devices. Android has its own back button, always available for the user; you do not need to put one in the action bar.

    – CommonsWare
    Aug 22 '12 at 12:39






  • 3





    Many apps today (2014) put a back/up button in the actionbar (eg. Instagram) so I even if this is an antipattern.. It is clearly a need in users (maybe users going from iPhone see more benefit on this)

    – sports
    Oct 6 '14 at 18:42











  • please check this answer

    – Amit Vaghela
    Sep 26 '16 at 11:58










5




5





Up button it not them same with back button. And i think add back button is anti Android pattern

– Nam Trung
Aug 22 '12 at 10:18





Up button it not them same with back button. And i think add back button is anti Android pattern

– Nam Trung
Aug 22 '12 at 10:18




6




6





The screenshot is of an iPhone. Android does not run on such devices. Android has its own back button, always available for the user; you do not need to put one in the action bar.

– CommonsWare
Aug 22 '12 at 12:39





The screenshot is of an iPhone. Android does not run on such devices. Android has its own back button, always available for the user; you do not need to put one in the action bar.

– CommonsWare
Aug 22 '12 at 12:39




3




3





Many apps today (2014) put a back/up button in the actionbar (eg. Instagram) so I even if this is an antipattern.. It is clearly a need in users (maybe users going from iPhone see more benefit on this)

– sports
Oct 6 '14 at 18:42





Many apps today (2014) put a back/up button in the actionbar (eg. Instagram) so I even if this is an antipattern.. It is clearly a need in users (maybe users going from iPhone see more benefit on this)

– sports
Oct 6 '14 at 18:42













please check this answer

– Amit Vaghela
Sep 26 '16 at 11:58







please check this answer

– Amit Vaghela
Sep 26 '16 at 11:58














11 Answers
11






active

oldest

votes


















127














After setting
actionBar.setHomeButtonEnabled(true);



Add the following code:



@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}





share|improve this answer





















  • 15





    That's a bit cavalier, you have to test that menuItem == android.R.id.home before taking that action (usually in a switch statement along with other options for that activity or fragment). For API >= 16, you can also simply define the parent activity in the manifest

    – RedGlyph
    Sep 22 '13 at 12:52






  • 1





    this crashed my android app

    – slier
    Jan 12 '15 at 16:58











  • @slier, my app crashed too, and I found that actionbar was returning null

    – Akeshwar Jha
    Dec 3 '15 at 6:22






  • 12





    use getSupportActionBar() instead

    – John
    Dec 19 '15 at 8:14





















54














Add



actionBar.setHomeButtonEnabled(true);


and then add the following



@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}


As suggested by naXa I've added a check on the itemId, to have it work correctly in case there are multiple buttons on the action bar.






share|improve this answer


























  • Where's a check that the selected option is Home button?

    – naXa
    Dec 21 '15 at 14:52











  • I'm not sure. You might ask a specific question about that! :)

    – clami219
    Dec 21 '15 at 22:40











  • I mean, if there are other options in menu (for example, Help) they will be handled by onOptionsItemSelected() too, so we need to check menuItem.getItemId() == android.R.id.home

    – naXa
    Dec 22 '15 at 5:48











  • You are right! Thanks for that... I'll fix immediately the code

    – clami219
    Dec 22 '15 at 10:24



















26














this one worked for me:



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ... other stuff
}

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


The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.






share|improve this answer



















  • 2





    onSupportNavigateUp() is I think best method to do so.... Thanks

    – Inzimam Tariq IT
    May 12 '16 at 11:11



















24














After setting



 actionBar.setHomeButtonEnabled(true);


You have to configure the parent activity in your AndroidManifest.xml



<activity
android:name="com.example.MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" />
<activity
android:name="com.example.SecondActivity"
android:theme="@style/Theme.AppCompat" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>


Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html






share|improve this answer



















  • 1





    This the cleaner route. Also, good link to implementing from android.

    – Kalel Wade
    Feb 27 '14 at 20:39






  • 1





    I suspect this will not work if your child activity has more than one parent activity. i.e. more than one activity has the ability to navigate to the child activity.

    – Scott Ferguson
    May 26 '16 at 4:35



















10














There are two ways to approach this.



Option 1: Update the Android Manifest
If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar



<activity
android:name=".SettingsActivity"
android:label="Setting Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.example.MainActivity" />
</activity>


Option 2: Change a setting for the ActionBar
If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your @imports match the level of compatibility you are looking for).



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_test);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}


Then, detect the 'back button' press and tell Android to close the currently open Activity.



@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}


That should do it!






share|improve this answer































    6














    You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method



    Duplicate of Android Sherlock ActionBar Up button






    share|improve this answer

































      6














      Simpler and better:
      For API >= 16



      Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.



      <activity
      android:name="com.example.myfirstapp.DisplayMessageActivity"
      android:label="@string/title_activity_display_message"
      android:parentActivityName="com.example.myfirstapp.MainActivity" >





      share|improve this answer
























      • This doesn't work with me. It creates a back button on Actionbar and it goes to the parent Activity. But the parent Activity works nothing.

        – c-an
        Dec 4 '18 at 18:20





















      6














      Use this to show back button and move to previous activity,



      final ActionBar actionBar = getSupportActionBar();
      assert actionBar != null;
      actionBar.setDisplayHomeAsUpEnabled(true);
      actionBar.setHomeAsUpIndicator(R.drawable.back_dark);


      @Override
      public boolean onOptionsItemSelected(final MenuItem item) {

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





      share|improve this answer































        6














        Firstly Use this:



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


        Then set operation of button click in onOptionsItemSelected method



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





        share|improve this answer


























        • is following should be there ? @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); }

          – Shrenik
          Jul 24 '15 at 13:27













        • If you need options in menu then it should be, otherwise not important.

          – Arunendra
          Jul 27 '15 at 6:11













        • I am using AppCompatActivity, and onOptionsItemSelected is there <br/> but the case case android.R.id.home: does not get called , any hint ?

          – Shrenik
          Jul 28 '15 at 6:00













        • All Methods are Not working in My Case Because i am using Fragment in

          – Ashish Shahi
          Feb 20 '17 at 13:17



















        5














        if anyone else need the solution



        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == android.R.id.home) {
        onBackPressed();
        }

        return super.onOptionsItemSelected(item);
        }





        share|improve this answer

































          1














          Add this line in onCreate() method



          getSupportActionBar().setDisplayHomeAsUpEnabled(true);


          then Override this method



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





          share|improve this answer






















            protected by Community Jan 4 '18 at 19:17



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?














            11 Answers
            11






            active

            oldest

            votes








            11 Answers
            11






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            127














            After setting
            actionBar.setHomeButtonEnabled(true);



            Add the following code:



            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
            // app icon in action bar clicked; goto parent activity.
            this.finish();
            return true;
            default:
            return super.onOptionsItemSelected(item);
            }
            }





            share|improve this answer





















            • 15





              That's a bit cavalier, you have to test that menuItem == android.R.id.home before taking that action (usually in a switch statement along with other options for that activity or fragment). For API >= 16, you can also simply define the parent activity in the manifest

              – RedGlyph
              Sep 22 '13 at 12:52






            • 1





              this crashed my android app

              – slier
              Jan 12 '15 at 16:58











            • @slier, my app crashed too, and I found that actionbar was returning null

              – Akeshwar Jha
              Dec 3 '15 at 6:22






            • 12





              use getSupportActionBar() instead

              – John
              Dec 19 '15 at 8:14


















            127














            After setting
            actionBar.setHomeButtonEnabled(true);



            Add the following code:



            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
            // app icon in action bar clicked; goto parent activity.
            this.finish();
            return true;
            default:
            return super.onOptionsItemSelected(item);
            }
            }





            share|improve this answer





















            • 15





              That's a bit cavalier, you have to test that menuItem == android.R.id.home before taking that action (usually in a switch statement along with other options for that activity or fragment). For API >= 16, you can also simply define the parent activity in the manifest

              – RedGlyph
              Sep 22 '13 at 12:52






            • 1





              this crashed my android app

              – slier
              Jan 12 '15 at 16:58











            • @slier, my app crashed too, and I found that actionbar was returning null

              – Akeshwar Jha
              Dec 3 '15 at 6:22






            • 12





              use getSupportActionBar() instead

              – John
              Dec 19 '15 at 8:14
















            127












            127








            127







            After setting
            actionBar.setHomeButtonEnabled(true);



            Add the following code:



            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
            // app icon in action bar clicked; goto parent activity.
            this.finish();
            return true;
            default:
            return super.onOptionsItemSelected(item);
            }
            }





            share|improve this answer















            After setting
            actionBar.setHomeButtonEnabled(true);



            Add the following code:



            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
            // app icon in action bar clicked; goto parent activity.
            this.finish();
            return true;
            default:
            return super.onOptionsItemSelected(item);
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 27 '14 at 7:56









            JaydeepW

            2,54412026




            2,54412026










            answered Dec 6 '12 at 10:04









            DaneshDanesh

            1,4281119




            1,4281119








            • 15





              That's a bit cavalier, you have to test that menuItem == android.R.id.home before taking that action (usually in a switch statement along with other options for that activity or fragment). For API >= 16, you can also simply define the parent activity in the manifest

              – RedGlyph
              Sep 22 '13 at 12:52






            • 1





              this crashed my android app

              – slier
              Jan 12 '15 at 16:58











            • @slier, my app crashed too, and I found that actionbar was returning null

              – Akeshwar Jha
              Dec 3 '15 at 6:22






            • 12





              use getSupportActionBar() instead

              – John
              Dec 19 '15 at 8:14
















            • 15





              That's a bit cavalier, you have to test that menuItem == android.R.id.home before taking that action (usually in a switch statement along with other options for that activity or fragment). For API >= 16, you can also simply define the parent activity in the manifest

              – RedGlyph
              Sep 22 '13 at 12:52






            • 1





              this crashed my android app

              – slier
              Jan 12 '15 at 16:58











            • @slier, my app crashed too, and I found that actionbar was returning null

              – Akeshwar Jha
              Dec 3 '15 at 6:22






            • 12





              use getSupportActionBar() instead

              – John
              Dec 19 '15 at 8:14










            15




            15





            That's a bit cavalier, you have to test that menuItem == android.R.id.home before taking that action (usually in a switch statement along with other options for that activity or fragment). For API >= 16, you can also simply define the parent activity in the manifest

            – RedGlyph
            Sep 22 '13 at 12:52





            That's a bit cavalier, you have to test that menuItem == android.R.id.home before taking that action (usually in a switch statement along with other options for that activity or fragment). For API >= 16, you can also simply define the parent activity in the manifest

            – RedGlyph
            Sep 22 '13 at 12:52




            1




            1





            this crashed my android app

            – slier
            Jan 12 '15 at 16:58





            this crashed my android app

            – slier
            Jan 12 '15 at 16:58













            @slier, my app crashed too, and I found that actionbar was returning null

            – Akeshwar Jha
            Dec 3 '15 at 6:22





            @slier, my app crashed too, and I found that actionbar was returning null

            – Akeshwar Jha
            Dec 3 '15 at 6:22




            12




            12





            use getSupportActionBar() instead

            – John
            Dec 19 '15 at 8:14







            use getSupportActionBar() instead

            – John
            Dec 19 '15 at 8:14















            54














            Add



            actionBar.setHomeButtonEnabled(true);


            and then add the following



            @Override
            public boolean onOptionsItemSelected(MenuItem menuItem)
            {
            switch (menuItem.getItemId()) {
            case android.R.id.home:
            onBackPressed();
            return true;
            default:
            return super.onOptionsItemSelected(menuItem);
            }
            }


            As suggested by naXa I've added a check on the itemId, to have it work correctly in case there are multiple buttons on the action bar.






            share|improve this answer


























            • Where's a check that the selected option is Home button?

              – naXa
              Dec 21 '15 at 14:52











            • I'm not sure. You might ask a specific question about that! :)

              – clami219
              Dec 21 '15 at 22:40











            • I mean, if there are other options in menu (for example, Help) they will be handled by onOptionsItemSelected() too, so we need to check menuItem.getItemId() == android.R.id.home

              – naXa
              Dec 22 '15 at 5:48











            • You are right! Thanks for that... I'll fix immediately the code

              – clami219
              Dec 22 '15 at 10:24
















            54














            Add



            actionBar.setHomeButtonEnabled(true);


            and then add the following



            @Override
            public boolean onOptionsItemSelected(MenuItem menuItem)
            {
            switch (menuItem.getItemId()) {
            case android.R.id.home:
            onBackPressed();
            return true;
            default:
            return super.onOptionsItemSelected(menuItem);
            }
            }


            As suggested by naXa I've added a check on the itemId, to have it work correctly in case there are multiple buttons on the action bar.






            share|improve this answer


























            • Where's a check that the selected option is Home button?

              – naXa
              Dec 21 '15 at 14:52











            • I'm not sure. You might ask a specific question about that! :)

              – clami219
              Dec 21 '15 at 22:40











            • I mean, if there are other options in menu (for example, Help) they will be handled by onOptionsItemSelected() too, so we need to check menuItem.getItemId() == android.R.id.home

              – naXa
              Dec 22 '15 at 5:48











            • You are right! Thanks for that... I'll fix immediately the code

              – clami219
              Dec 22 '15 at 10:24














            54












            54








            54







            Add



            actionBar.setHomeButtonEnabled(true);


            and then add the following



            @Override
            public boolean onOptionsItemSelected(MenuItem menuItem)
            {
            switch (menuItem.getItemId()) {
            case android.R.id.home:
            onBackPressed();
            return true;
            default:
            return super.onOptionsItemSelected(menuItem);
            }
            }


            As suggested by naXa I've added a check on the itemId, to have it work correctly in case there are multiple buttons on the action bar.






            share|improve this answer















            Add



            actionBar.setHomeButtonEnabled(true);


            and then add the following



            @Override
            public boolean onOptionsItemSelected(MenuItem menuItem)
            {
            switch (menuItem.getItemId()) {
            case android.R.id.home:
            onBackPressed();
            return true;
            default:
            return super.onOptionsItemSelected(menuItem);
            }
            }


            As suggested by naXa I've added a check on the itemId, to have it work correctly in case there are multiple buttons on the action bar.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:17









            Community

            11




            11










            answered Jul 1 '13 at 16:10









            clami219clami219

            2,46512138




            2,46512138













            • Where's a check that the selected option is Home button?

              – naXa
              Dec 21 '15 at 14:52











            • I'm not sure. You might ask a specific question about that! :)

              – clami219
              Dec 21 '15 at 22:40











            • I mean, if there are other options in menu (for example, Help) they will be handled by onOptionsItemSelected() too, so we need to check menuItem.getItemId() == android.R.id.home

              – naXa
              Dec 22 '15 at 5:48











            • You are right! Thanks for that... I'll fix immediately the code

              – clami219
              Dec 22 '15 at 10:24



















            • Where's a check that the selected option is Home button?

              – naXa
              Dec 21 '15 at 14:52











            • I'm not sure. You might ask a specific question about that! :)

              – clami219
              Dec 21 '15 at 22:40











            • I mean, if there are other options in menu (for example, Help) they will be handled by onOptionsItemSelected() too, so we need to check menuItem.getItemId() == android.R.id.home

              – naXa
              Dec 22 '15 at 5:48











            • You are right! Thanks for that... I'll fix immediately the code

              – clami219
              Dec 22 '15 at 10:24

















            Where's a check that the selected option is Home button?

            – naXa
            Dec 21 '15 at 14:52





            Where's a check that the selected option is Home button?

            – naXa
            Dec 21 '15 at 14:52













            I'm not sure. You might ask a specific question about that! :)

            – clami219
            Dec 21 '15 at 22:40





            I'm not sure. You might ask a specific question about that! :)

            – clami219
            Dec 21 '15 at 22:40













            I mean, if there are other options in menu (for example, Help) they will be handled by onOptionsItemSelected() too, so we need to check menuItem.getItemId() == android.R.id.home

            – naXa
            Dec 22 '15 at 5:48





            I mean, if there are other options in menu (for example, Help) they will be handled by onOptionsItemSelected() too, so we need to check menuItem.getItemId() == android.R.id.home

            – naXa
            Dec 22 '15 at 5:48













            You are right! Thanks for that... I'll fix immediately the code

            – clami219
            Dec 22 '15 at 10:24





            You are right! Thanks for that... I'll fix immediately the code

            – clami219
            Dec 22 '15 at 10:24











            26














            this one worked for me:



            @Override
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_your_activity);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            // ... other stuff
            }

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


            The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.






            share|improve this answer



















            • 2





              onSupportNavigateUp() is I think best method to do so.... Thanks

              – Inzimam Tariq IT
              May 12 '16 at 11:11
















            26














            this one worked for me:



            @Override
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_your_activity);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            // ... other stuff
            }

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


            The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.






            share|improve this answer



















            • 2





              onSupportNavigateUp() is I think best method to do so.... Thanks

              – Inzimam Tariq IT
              May 12 '16 at 11:11














            26












            26








            26







            this one worked for me:



            @Override
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_your_activity);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            // ... other stuff
            }

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


            The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.






            share|improve this answer













            this one worked for me:



            @Override
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_your_activity);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            // ... other stuff
            }

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


            The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 20 '15 at 20:25









            Jason SaruuloJason Saruulo

            1,12131320




            1,12131320








            • 2





              onSupportNavigateUp() is I think best method to do so.... Thanks

              – Inzimam Tariq IT
              May 12 '16 at 11:11














            • 2





              onSupportNavigateUp() is I think best method to do so.... Thanks

              – Inzimam Tariq IT
              May 12 '16 at 11:11








            2




            2





            onSupportNavigateUp() is I think best method to do so.... Thanks

            – Inzimam Tariq IT
            May 12 '16 at 11:11





            onSupportNavigateUp() is I think best method to do so.... Thanks

            – Inzimam Tariq IT
            May 12 '16 at 11:11











            24














            After setting



             actionBar.setHomeButtonEnabled(true);


            You have to configure the parent activity in your AndroidManifest.xml



            <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat" />
            <activity
            android:name="com.example.SecondActivity"
            android:theme="@style/Theme.AppCompat" >
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.MainActivity" />
            </activity>


            Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html






            share|improve this answer



















            • 1





              This the cleaner route. Also, good link to implementing from android.

              – Kalel Wade
              Feb 27 '14 at 20:39






            • 1





              I suspect this will not work if your child activity has more than one parent activity. i.e. more than one activity has the ability to navigate to the child activity.

              – Scott Ferguson
              May 26 '16 at 4:35
















            24














            After setting



             actionBar.setHomeButtonEnabled(true);


            You have to configure the parent activity in your AndroidManifest.xml



            <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat" />
            <activity
            android:name="com.example.SecondActivity"
            android:theme="@style/Theme.AppCompat" >
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.MainActivity" />
            </activity>


            Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html






            share|improve this answer



















            • 1





              This the cleaner route. Also, good link to implementing from android.

              – Kalel Wade
              Feb 27 '14 at 20:39






            • 1





              I suspect this will not work if your child activity has more than one parent activity. i.e. more than one activity has the ability to navigate to the child activity.

              – Scott Ferguson
              May 26 '16 at 4:35














            24












            24








            24







            After setting



             actionBar.setHomeButtonEnabled(true);


            You have to configure the parent activity in your AndroidManifest.xml



            <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat" />
            <activity
            android:name="com.example.SecondActivity"
            android:theme="@style/Theme.AppCompat" >
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.MainActivity" />
            </activity>


            Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html






            share|improve this answer













            After setting



             actionBar.setHomeButtonEnabled(true);


            You have to configure the parent activity in your AndroidManifest.xml



            <activity
            android:name="com.example.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat" />
            <activity
            android:name="com.example.SecondActivity"
            android:theme="@style/Theme.AppCompat" >
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.MainActivity" />
            </activity>


            Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 7 '13 at 15:45









            BoschAlexBoschAlex

            24124




            24124








            • 1





              This the cleaner route. Also, good link to implementing from android.

              – Kalel Wade
              Feb 27 '14 at 20:39






            • 1





              I suspect this will not work if your child activity has more than one parent activity. i.e. more than one activity has the ability to navigate to the child activity.

              – Scott Ferguson
              May 26 '16 at 4:35














            • 1





              This the cleaner route. Also, good link to implementing from android.

              – Kalel Wade
              Feb 27 '14 at 20:39






            • 1





              I suspect this will not work if your child activity has more than one parent activity. i.e. more than one activity has the ability to navigate to the child activity.

              – Scott Ferguson
              May 26 '16 at 4:35








            1




            1





            This the cleaner route. Also, good link to implementing from android.

            – Kalel Wade
            Feb 27 '14 at 20:39





            This the cleaner route. Also, good link to implementing from android.

            – Kalel Wade
            Feb 27 '14 at 20:39




            1




            1





            I suspect this will not work if your child activity has more than one parent activity. i.e. more than one activity has the ability to navigate to the child activity.

            – Scott Ferguson
            May 26 '16 at 4:35





            I suspect this will not work if your child activity has more than one parent activity. i.e. more than one activity has the ability to navigate to the child activity.

            – Scott Ferguson
            May 26 '16 at 4:35











            10














            There are two ways to approach this.



            Option 1: Update the Android Manifest
            If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar



            <activity
            android:name=".SettingsActivity"
            android:label="Setting Activity">
            <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.example.MainActivity" />
            </activity>


            Option 2: Change a setting for the ActionBar
            If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your @imports match the level of compatibility you are looking for).



            @Override
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings_test);
            ActionBar actionBar = getSupportActionBar();
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            }


            Then, detect the 'back button' press and tell Android to close the currently open Activity.



            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
            // app icon in action bar clicked; goto parent activity.
            this.finish();
            return true;
            default:
            return super.onOptionsItemSelected(item);
            }
            }


            That should do it!






            share|improve this answer




























              10














              There are two ways to approach this.



              Option 1: Update the Android Manifest
              If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar



              <activity
              android:name=".SettingsActivity"
              android:label="Setting Activity">
              <meta-data
              android:name="android.support.PARENT_ACTIVITY"
              android:value="com.example.example.MainActivity" />
              </activity>


              Option 2: Change a setting for the ActionBar
              If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your @imports match the level of compatibility you are looking for).



              @Override
              protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_settings_test);
              ActionBar actionBar = getSupportActionBar();
              actionBar.setHomeButtonEnabled(true);
              actionBar.setDisplayHomeAsUpEnabled(true);
              }


              Then, detect the 'back button' press and tell Android to close the currently open Activity.



              @Override
              public boolean onOptionsItemSelected(MenuItem item) {
              switch (item.getItemId()) {
              case android.R.id.home:
              // app icon in action bar clicked; goto parent activity.
              this.finish();
              return true;
              default:
              return super.onOptionsItemSelected(item);
              }
              }


              That should do it!






              share|improve this answer


























                10












                10








                10







                There are two ways to approach this.



                Option 1: Update the Android Manifest
                If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar



                <activity
                android:name=".SettingsActivity"
                android:label="Setting Activity">
                <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.example.MainActivity" />
                </activity>


                Option 2: Change a setting for the ActionBar
                If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your @imports match the level of compatibility you are looking for).



                @Override
                protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_settings_test);
                ActionBar actionBar = getSupportActionBar();
                actionBar.setHomeButtonEnabled(true);
                actionBar.setDisplayHomeAsUpEnabled(true);
                }


                Then, detect the 'back button' press and tell Android to close the currently open Activity.



                @Override
                public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case android.R.id.home:
                // app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
                default:
                return super.onOptionsItemSelected(item);
                }
                }


                That should do it!






                share|improve this answer













                There are two ways to approach this.



                Option 1: Update the Android Manifest
                If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar



                <activity
                android:name=".SettingsActivity"
                android:label="Setting Activity">
                <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.example.MainActivity" />
                </activity>


                Option 2: Change a setting for the ActionBar
                If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your @imports match the level of compatibility you are looking for).



                @Override
                protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_settings_test);
                ActionBar actionBar = getSupportActionBar();
                actionBar.setHomeButtonEnabled(true);
                actionBar.setDisplayHomeAsUpEnabled(true);
                }


                Then, detect the 'back button' press and tell Android to close the currently open Activity.



                @Override
                public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case android.R.id.home:
                // app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
                default:
                return super.onOptionsItemSelected(item);
                }
                }


                That should do it!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 10 '15 at 4:28









                Von IobroVon Iobro

                11113




                11113























                    6














                    You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method



                    Duplicate of Android Sherlock ActionBar Up button






                    share|improve this answer






























                      6














                      You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method



                      Duplicate of Android Sherlock ActionBar Up button






                      share|improve this answer




























                        6












                        6








                        6







                        You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method



                        Duplicate of Android Sherlock ActionBar Up button






                        share|improve this answer















                        You'll need to check menuItem.getItemId() against android.R.id.home in the onOptionsItemSelected method



                        Duplicate of Android Sherlock ActionBar Up button







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 23 '17 at 12:34









                        Community

                        11




                        11










                        answered Feb 1 '13 at 23:11









                        advantejadvantej

                        17k42837




                        17k42837























                            6














                            Simpler and better:
                            For API >= 16



                            Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.



                            <activity
                            android:name="com.example.myfirstapp.DisplayMessageActivity"
                            android:label="@string/title_activity_display_message"
                            android:parentActivityName="com.example.myfirstapp.MainActivity" >





                            share|improve this answer
























                            • This doesn't work with me. It creates a back button on Actionbar and it goes to the parent Activity. But the parent Activity works nothing.

                              – c-an
                              Dec 4 '18 at 18:20


















                            6














                            Simpler and better:
                            For API >= 16



                            Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.



                            <activity
                            android:name="com.example.myfirstapp.DisplayMessageActivity"
                            android:label="@string/title_activity_display_message"
                            android:parentActivityName="com.example.myfirstapp.MainActivity" >





                            share|improve this answer
























                            • This doesn't work with me. It creates a back button on Actionbar and it goes to the parent Activity. But the parent Activity works nothing.

                              – c-an
                              Dec 4 '18 at 18:20
















                            6












                            6








                            6







                            Simpler and better:
                            For API >= 16



                            Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.



                            <activity
                            android:name="com.example.myfirstapp.DisplayMessageActivity"
                            android:label="@string/title_activity_display_message"
                            android:parentActivityName="com.example.myfirstapp.MainActivity" >





                            share|improve this answer













                            Simpler and better:
                            For API >= 16



                            Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.



                            <activity
                            android:name="com.example.myfirstapp.DisplayMessageActivity"
                            android:label="@string/title_activity_display_message"
                            android:parentActivityName="com.example.myfirstapp.MainActivity" >






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 18 '15 at 9:45









                            M. Usman KhanM. Usman Khan

                            4,61513547




                            4,61513547













                            • This doesn't work with me. It creates a back button on Actionbar and it goes to the parent Activity. But the parent Activity works nothing.

                              – c-an
                              Dec 4 '18 at 18:20





















                            • This doesn't work with me. It creates a back button on Actionbar and it goes to the parent Activity. But the parent Activity works nothing.

                              – c-an
                              Dec 4 '18 at 18:20



















                            This doesn't work with me. It creates a back button on Actionbar and it goes to the parent Activity. But the parent Activity works nothing.

                            – c-an
                            Dec 4 '18 at 18:20







                            This doesn't work with me. It creates a back button on Actionbar and it goes to the parent Activity. But the parent Activity works nothing.

                            – c-an
                            Dec 4 '18 at 18:20













                            6














                            Use this to show back button and move to previous activity,



                            final ActionBar actionBar = getSupportActionBar();
                            assert actionBar != null;
                            actionBar.setDisplayHomeAsUpEnabled(true);
                            actionBar.setHomeAsUpIndicator(R.drawable.back_dark);


                            @Override
                            public boolean onOptionsItemSelected(final MenuItem item) {

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





                            share|improve this answer




























                              6














                              Use this to show back button and move to previous activity,



                              final ActionBar actionBar = getSupportActionBar();
                              assert actionBar != null;
                              actionBar.setDisplayHomeAsUpEnabled(true);
                              actionBar.setHomeAsUpIndicator(R.drawable.back_dark);


                              @Override
                              public boolean onOptionsItemSelected(final MenuItem item) {

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





                              share|improve this answer


























                                6












                                6








                                6







                                Use this to show back button and move to previous activity,



                                final ActionBar actionBar = getSupportActionBar();
                                assert actionBar != null;
                                actionBar.setDisplayHomeAsUpEnabled(true);
                                actionBar.setHomeAsUpIndicator(R.drawable.back_dark);


                                @Override
                                public boolean onOptionsItemSelected(final MenuItem item) {

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





                                share|improve this answer













                                Use this to show back button and move to previous activity,



                                final ActionBar actionBar = getSupportActionBar();
                                assert actionBar != null;
                                actionBar.setDisplayHomeAsUpEnabled(true);
                                actionBar.setHomeAsUpIndicator(R.drawable.back_dark);


                                @Override
                                public boolean onOptionsItemSelected(final MenuItem item) {

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






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Dec 22 '15 at 10:34









                                Amit VaghelaAmit Vaghela

                                17.5k1558114




                                17.5k1558114























                                    6














                                    Firstly Use this:



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


                                    Then set operation of button click in onOptionsItemSelected method



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





                                    share|improve this answer


























                                    • is following should be there ? @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); }

                                      – Shrenik
                                      Jul 24 '15 at 13:27













                                    • If you need options in menu then it should be, otherwise not important.

                                      – Arunendra
                                      Jul 27 '15 at 6:11













                                    • I am using AppCompatActivity, and onOptionsItemSelected is there <br/> but the case case android.R.id.home: does not get called , any hint ?

                                      – Shrenik
                                      Jul 28 '15 at 6:00













                                    • All Methods are Not working in My Case Because i am using Fragment in

                                      – Ashish Shahi
                                      Feb 20 '17 at 13:17
















                                    6














                                    Firstly Use this:



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


                                    Then set operation of button click in onOptionsItemSelected method



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





                                    share|improve this answer


























                                    • is following should be there ? @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); }

                                      – Shrenik
                                      Jul 24 '15 at 13:27













                                    • If you need options in menu then it should be, otherwise not important.

                                      – Arunendra
                                      Jul 27 '15 at 6:11













                                    • I am using AppCompatActivity, and onOptionsItemSelected is there <br/> but the case case android.R.id.home: does not get called , any hint ?

                                      – Shrenik
                                      Jul 28 '15 at 6:00













                                    • All Methods are Not working in My Case Because i am using Fragment in

                                      – Ashish Shahi
                                      Feb 20 '17 at 13:17














                                    6












                                    6








                                    6







                                    Firstly Use this:



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


                                    Then set operation of button click in onOptionsItemSelected method



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





                                    share|improve this answer















                                    Firstly Use this:



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


                                    Then set operation of button click in onOptionsItemSelected method



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






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 29 '18 at 6:24









                                    adiga

                                    12.2k62645




                                    12.2k62645










                                    answered Jan 20 '15 at 14:03









                                    ArunendraArunendra

                                    1,4421713




                                    1,4421713













                                    • is following should be there ? @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); }

                                      – Shrenik
                                      Jul 24 '15 at 13:27













                                    • If you need options in menu then it should be, otherwise not important.

                                      – Arunendra
                                      Jul 27 '15 at 6:11













                                    • I am using AppCompatActivity, and onOptionsItemSelected is there <br/> but the case case android.R.id.home: does not get called , any hint ?

                                      – Shrenik
                                      Jul 28 '15 at 6:00













                                    • All Methods are Not working in My Case Because i am using Fragment in

                                      – Ashish Shahi
                                      Feb 20 '17 at 13:17



















                                    • is following should be there ? @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); }

                                      – Shrenik
                                      Jul 24 '15 at 13:27













                                    • If you need options in menu then it should be, otherwise not important.

                                      – Arunendra
                                      Jul 27 '15 at 6:11













                                    • I am using AppCompatActivity, and onOptionsItemSelected is there <br/> but the case case android.R.id.home: does not get called , any hint ?

                                      – Shrenik
                                      Jul 28 '15 at 6:00













                                    • All Methods are Not working in My Case Because i am using Fragment in

                                      – Ashish Shahi
                                      Feb 20 '17 at 13:17

















                                    is following should be there ? @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); }

                                    – Shrenik
                                    Jul 24 '15 at 13:27







                                    is following should be there ? @Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); }

                                    – Shrenik
                                    Jul 24 '15 at 13:27















                                    If you need options in menu then it should be, otherwise not important.

                                    – Arunendra
                                    Jul 27 '15 at 6:11







                                    If you need options in menu then it should be, otherwise not important.

                                    – Arunendra
                                    Jul 27 '15 at 6:11















                                    I am using AppCompatActivity, and onOptionsItemSelected is there <br/> but the case case android.R.id.home: does not get called , any hint ?

                                    – Shrenik
                                    Jul 28 '15 at 6:00







                                    I am using AppCompatActivity, and onOptionsItemSelected is there <br/> but the case case android.R.id.home: does not get called , any hint ?

                                    – Shrenik
                                    Jul 28 '15 at 6:00















                                    All Methods are Not working in My Case Because i am using Fragment in

                                    – Ashish Shahi
                                    Feb 20 '17 at 13:17





                                    All Methods are Not working in My Case Because i am using Fragment in

                                    – Ashish Shahi
                                    Feb 20 '17 at 13:17











                                    5














                                    if anyone else need the solution



                                    @Override
                                    public boolean onOptionsItemSelected(MenuItem item) {
                                    int id = item.getItemId();

                                    if (id == android.R.id.home) {
                                    onBackPressed();
                                    }

                                    return super.onOptionsItemSelected(item);
                                    }





                                    share|improve this answer






























                                      5














                                      if anyone else need the solution



                                      @Override
                                      public boolean onOptionsItemSelected(MenuItem item) {
                                      int id = item.getItemId();

                                      if (id == android.R.id.home) {
                                      onBackPressed();
                                      }

                                      return super.onOptionsItemSelected(item);
                                      }





                                      share|improve this answer




























                                        5












                                        5








                                        5







                                        if anyone else need the solution



                                        @Override
                                        public boolean onOptionsItemSelected(MenuItem item) {
                                        int id = item.getItemId();

                                        if (id == android.R.id.home) {
                                        onBackPressed();
                                        }

                                        return super.onOptionsItemSelected(item);
                                        }





                                        share|improve this answer















                                        if anyone else need the solution



                                        @Override
                                        public boolean onOptionsItemSelected(MenuItem item) {
                                        int id = item.getItemId();

                                        if (id == android.R.id.home) {
                                        onBackPressed();
                                        }

                                        return super.onOptionsItemSelected(item);
                                        }






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Feb 24 '15 at 9:20

























                                        answered Feb 24 '15 at 9:15









                                        Saad MahmudSaad Mahmud

                                        2,15111721




                                        2,15111721























                                            1














                                            Add this line in onCreate() method



                                            getSupportActionBar().setDisplayHomeAsUpEnabled(true);


                                            then Override this method



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





                                            share|improve this answer




























                                              1














                                              Add this line in onCreate() method



                                              getSupportActionBar().setDisplayHomeAsUpEnabled(true);


                                              then Override this method



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





                                              share|improve this answer


























                                                1












                                                1








                                                1







                                                Add this line in onCreate() method



                                                getSupportActionBar().setDisplayHomeAsUpEnabled(true);


                                                then Override this method



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





                                                share|improve this answer













                                                Add this line in onCreate() method



                                                getSupportActionBar().setDisplayHomeAsUpEnabled(true);


                                                then Override this method



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






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Apr 17 '17 at 18:11









                                                TsanguuTsanguu

                                                112




                                                112

















                                                    protected by Community Jan 4 '18 at 19:17



                                                    Thank you for your interest in this question.
                                                    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                    Would you like to answer one of these unanswered questions instead?



                                                    Popular posts from this blog

                                                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                                    Calculate evaluation metrics using cross_val_predict sklearn

                                                    Insert data from modal to MySQL (multiple modal on website)