Problems with getSupportActionBar() to Home (arrow) button and back arrow

Asked

Viewed 3,605 times

0

I’m having trouble giving the action to the home button, I’m managing to implement without problems the button according to the code

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

But I don’t know how to define to which Activity he should go and how to make him "kill" the Activity where you are. The described so far is indicated in the image below as 1

I would also like help with the described as 2, it works to get back on screen (Activity) previous, but I’d like to set it to go back on the main screen but haven’t found any method to treat this button.

The idea is, the button 1 go back to the Activity previous and button 2 go back to the Activity leading "Home" and that whenever you return to any place the closed page is "killed", thus not consuming processing with what would not be used.

3 answers

3


To reset what happens when you click the button Back of Navigation Bar (2), you must rewrite the following method in your Activity:

@Override
public void onBackPressed(){

    Intent mIntent = //Crie a intent para chamar a activity principal

    startActivity(mIntent);

    finish(); // Finaliza a Activity atual

    return;
}

To set what happens when you click the button Back of Action Bar (1) you must write the following code on your Activity

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

     switch (item.getItemId()) {

         case android.R.id.home:

             Intent mIntent = //Crie a intent para chamar a activity anterior

             startActivity(mIntent);

             finish(); // Finaliza a Activity atual

         break;

         default:break;
    }

    return true;
}

In case you return to the previous Activity, in some cases you can simply call the method finish(), because then the current Activity will be finalized and the previous Activity will be called.

  • How could I set an ID to the top button (1)? Whereas I get it calling function: getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 getSupportActionBar().setHomeButtonEnabled(true); E has no ID (at least not visible)... Thank you :)

  • @Guilhermehs, Look for the method onCreateOptionsMenu() in your Activity that displays Toolbar and see which layout file is used to define the menu. Then go to that file and see what the back button id is

  • I couldn’t find this method onCreateOptionsMenu() has some specific place that she gets?

  • It stays in the Activity that contains Toolbar. If you’re a little hard to find, open the Activity file and press Alt+7 to open the panel of class manure and then click on the icon of az at the top of that panel. Now just look for the method onCreateOptionsMenu()

  • So, that’s the problem, it’s not in mine Activity this method, look at the print how is mine Structure, what is selected with the mouse is what "invokes" the button 1

  • @Guilhermehs, already understood what you did, vc is not using a Toolbar, but rather the standard Android Action Bar. I reedited the answer. just change the id.do.botao.back.da.Toolbar for android.R.id.home

  • That I swear is the last question :/ ..... When I use the finish(); he does not "kill" the Activity that I am, it just goes to the other and it stays active, so much so that when it gets to the main screen if I press the button to return (2) he will start to actually close the screens that before he just exchanged for another, IE, he is just changing screen, the finish() is not having the effect of closing itself, it’s just replacing, if you know what I mean :/ (I’ve tried using the ActivityAtual.this.finish(); but makes a mistake.

Show 2 more comments

1

Anyway I believe this is the simplest.

In the onCreate method of Activity enter the following code:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Now comes the secret, in the file Androidmanifest define the father of your Activity, or wherever you want your Activity to go when priced "Action Bar (1)". Just add the following line: android:parentActivityName=". Homeactivity"

Ex:

<activity
        android:name=".ProdutosActivity"
        android:label="@string/title_activity_produtos"
        android:parentActivityName=".RankingActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar" />

0

Next guy, easy way, creates an xml and in it you create a Toolbar(instead of Linear or Relative Layout) but it will need to be like this(android.support.v7.widget.Toolbar), can be very simple:

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
   android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/cardview_light_background"
    android:elevation="4dp">

After creating you insert it into the xml you want it to appear, the one where the back button should appear:

<include
    android:id="@+id/app_bar"
    layout="@layout/menu_back_button"
    />

Note that I gave an id to include, now go to Activity where the menu should appear and enter the following code, just below onCreate:

 Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

Done this the bar should work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.