1

I want to set a custom toolbar with a back arrow (to go to the previous activity, which in this case is always back to main activity) for all activities except main activity.

As read from a couple of posts, I have created a custom toolbar activity and here is the code:

activity_toolbar_back_arrow.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ToolbarBackArrow">

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:title="My First App"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:id="@+id/toolbar_back_arrow"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"/>
</LinearLayout>

ToolbarBackArrow.class

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;

public class ToolbarBackArrow extends AppCompatActivity {

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

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

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}

Now an another activity is created that extends the above custom activity: VisionMission.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;

public class VisionMission extends ToolbarBackArrow {

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

}
}

In the activity_vision_mission.xml, I have some code specific to just that activity.

As per the solutions, I read this should create a toolbar in the VisionMission activity, but it is not. What am I missing? In manifest, I have made theme as No ActionBar for all the activities.

ar3
  • 369
  • 1
  • 4
  • 18
  • 2
    why dont you make a seperate layout containing the toolbar and just `include` it where ever u want – Pemba Tamang Sep 27 '19 at 09:03
  • Do we need to do both adding through include in xml and also in .java file extending the class or using setSupportActionBar()? – ar3 Sep 27 '19 at 09:35
  • 1
    just in the xml, include the layout and it will be there. You can even findviews inside the included layout and work on them. like this https://stackoverflow.com/a/4787064/8528047 – Pemba Tamang Sep 27 '19 at 09:38

2 Answers2

2

No need to go for custom toolbar if your only requirement is back button.

In your manifest.xml file for the specified activity,

<activity
    android:name="com.example.app_name.A" ...>
    ...
</activity>
<!-- A child of the main activity -->
<activity
    android:name=".B"
    android:label="B"
    android:parentActivityName="the activity to go on back click" >
    <!--android:parentActivityName="com.examle.myparentactivity" -->
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.app_name.A" />
</activity>

It will automatically show the back button with desired behaviour.

Malavan
  • 789
  • 7
  • 27
  • 1
    Man, you saved me! I have been scratching my head for a day. I did add this in the manifest file along with setting up of Toolbar, but did not know that just declaring that attribute android:parentActivityName="the activity to go on back click" would be enough. >Have a problem though, when the activity opens it slides in from right to left and when we click on back arrow, it should slide back from left to right. But it again slides from right to left!- How to resolve this? – ar3 Sep 27 '19 at 09:26
  • I have tried modifying the styles.xml adding custom anim, but the default open and close transitions are not coming. – ar3 Sep 27 '19 at 09:27
  • I found the solution to this animation/transition issue. I have to add the att 'android:launchMode="singleTop" ' to the '.MainActivity' (or whichever activity we have to go back) in the manifests.xml – ar3 Sep 28 '19 at 06:38
  • Happy you have found it. – Malavan Oct 01 '19 at 12:56
1

It's because in your ToolbarBackArrow you are setting your layout first and then in VisionMission you're resetting the layout and old layout with your toolbar is being replaced by another call to setContentView. To achieve desired result use includes:

include_toolbar.xml:

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:title="My First App"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:id="@+id/toolbar_back_arrow"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"/>

activity_vision_mission.xml:

<LinearLayout
    ...>

   <include layout="@layout/include_toolbar" />

   ... Your other views ...

</LinearLayout>

and in super class ToolbarBackArrow

public class ToolbarBackArrow extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Not needed anymore
        // setContentView(R.layout.activity_toolbar_back_arrow);

        Toolbar toolbar = findViewById(R.id.toolbar_back_arrow);

        // always check because you can forget to add an 'include' and
        // toolbar will be null here, so you'll get NullPointerException
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
}
GV_FiQst
  • 1,487
  • 14
  • 30
  • Thank you for the quick response. I did put that 'include' statement, but the thing is entire VisionMission is in a ScrollView --> Linear Layout --> include, the toolbar also scrolls with content. Should I make it LL --> include --> ScrollView --> LL instead? – ar3 Sep 27 '19 at 09:23
  • 1
    Yeah it may work. Actually I'm using this technique in one of production projects of mine. I have `LL -> (include)/toolbar, NestedScrollView -> ConstraintLayout -> otherviews` in all of fragments. – GV_FiQst Sep 27 '19 at 09:28