0

If I have 2 fragments, Fragment1 and Fragment2. How can I go back to Fragment1 from Fragment2 by using ActionBar? Below is my activity_main.xml

ActivityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@+id/fragment_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android.p2q4.MainActivity">

</FrameLayout>
Cayenne Teoh
  • 70
  • 2
  • 10

1 Answers1

0

Call setNavigationIcon() and then setNavigationIconOnClickListener and in the OnClick() set up the code for going to the previous fragment.

Note: You must use Toolbar for this.

user221256
  • 415
  • 5
  • 14
  • @yoyo97 If fragments have their own layout you can embed toolbars into them. Or just use LinearLayout as parent and add tooblar and FrameLayout as its childs. – user221256 Jul 20 '17 at 16:06