Problems with getSupportActionBar () for button (arrow) Home and back arrow ←

0

I am having trouble giving the action to the home button, I am able to smoothly implement the button according to the code ↓

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

But I do not know how to define what activity it should go to and how to "kill" the activity it is in. The one described so far is the one indicated in the image below as 1

Iwouldlikehelpalsowithwhatisdescribedas2,itworkstogobacktothepreviousscreen,butIwouldliketosetittoreturntothemainscreen,butIhavenotyetfoundanymethodtohandlethisbutton.

Theideaisforthe1buttontogobacktothepreviousactivityandthe2buttontogobacktotheactivityin>main" Home " and that whenever it returns to any place the closed page is "killed", thus not consuming processing with what would not be being used.     

asked by anonymous 15.04.2016 / 18:49

3 answers

3

To reset what happens when you click the Navigation Bar (2) button, you should 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 define what happens when you click the Action Bar button you should type the following code in 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 go back to the previous activity, in some cases you can simply call the finish() method, because then the current activity will be terminated and the previous activity will be called.

    
15.04.2016 / 19:34
0

Next, easy way, create an xml and in it you create a toolbar (instead of Linear or Relative Layout) but it will need to be this way ( android.support.v7.widget.Toolbar ), it 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 button should appear back:

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

Notice that I gave an include ID, now go to the 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);

Once this is done the bar should work.

    
15.04.2016 / 23:18
0

I believe this is the simplest anyway.

In the onCreate method of Activity enter the following code:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Now comes the secret, in the AndroidManifest file, set the parent of your activity, ie where you want your activity to go when preloaded "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" />
    
22.04.2016 / 06:01