How to put or enable back button in toolbar

1

something very strange is happening to me, I do not get the button to go back in the toolbar and in a previous project if I leave .. I followed the same steps with the difference that now the toolbar I have in a separate xml but not I know why I do not get anything xx

this is toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_height="wrap_content"
  android:layout_width="match_parent"
  android:minHeight="?attr/actionBarSize"
  android:background="?attr/colorPrimary"
  android:elevation="4dp"
  android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>

my MainActivity has a recyclerView, when I press an item I go to my second activity where I want the back button to come out but it does not turn out to be x.x

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/RelativeLayoutMain"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.pauli.applista.MainActivity">

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

  <android.support.v7.widget.RecyclerView
      android:id="@+id/reciclador"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:padding="3dp"
      android:scrollbars="vertical"
      android:layout_below="@+id/toolbar" />

</RelativeLayout>

In the manifest I have assigned the main activity as the father of the second

<activity
        android:name=".Segunda"
        android:parentActivityName=".MainActivity">
</activity>

In the theme of the application I have the code to enable my toolbar

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- habilitar toolbar -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Now in my MainActivity.java I assign the toolbar to appear

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

I still have the code to initialize recycler and all that .. in the method for the click of each item in the recyclerView I start the second activity.

@Override
public void onClick(ImagenAdapter.ViewHolder holder, int idImagen) {
    Intent intent = new Intent(MainActivity.this, Segunda.class);
    Bundle b = new Bundle();
    b.putInt("IMAGEN", idImagen);
    intent.putExtras(b);
    startActivity(intent);
}

In the xml of my second activity at the moment I only include the toolbar

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/activity_segunda"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.pauli.applista.Segunda">

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

</RelativeLayout>

And my file Segunda.java only has the onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_segunda);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

According to me this is fine and should work but I do not know why he does not want to leave the button .. I hope someone knows .. maybe it is something very silly that I am doing wrong x.x

    
asked by Pauli 19.11.2016 в 18:20
source

2 answers

1

Make sure your class Activity extends from AppCompactActivity , with this you can perform the loading of the ToolBar:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

I found how to do it you can see it in the documentation

To enable navigation, it is done by calling setDisplayHomeAsUpEnabled() :

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

If setDisplayHomeAsUpEnabled() is set to true if the selection of "home" returns to a single level in the UI instead of returning to the top level or first page.

    
answered by 19.11.2016 / 18:57
source
0

I already worked, I just had to write

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

instead of what I was putting at the beginning ... but now I would like to know what the difference is with

getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);

or I found others and there I started to try which one was

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
    
answered by 19.11.2016 в 20:56