1

I have a nav drawer which in its first fragment i have a news page where the user clicks a button to see the whole news, and at that moment it enters another fragment.

I want to know how I implement an arrow to return to the previous fragment instead of the icon that opens the Navigation Drawer.

I've never worked with this before, I have no idea.

This is my Toolbar in MainActivity

appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);

toolbar = (Toolbar) findViewById(R.id.toolbar);
//toolbar title
toolbar.setTitle(null);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// Toolbar logo
logoToolbar = (ImageView) findViewById(R.id.logoToolbar);
logoToolbar.setImageResource(R.drawable.newstoolbar);

When the user click in "Read More" in the MainFragment

newsMore.get(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);

                newsFrag1 newsFragment1 = new newsFrag1();
                ft.replace(R.id.fragment_container, newsFragment1);
                ft.commit();
            }
});

And then it goes to newsFrag1, which I want to have the back arrow

My Toolbar XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="studio.com.archeagemanager.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            android:gravity="center_vertical"
            android:minHeight="56dp"
            android:weightSum="1"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <ImageView
                android:id="@+id/logoToolbar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="56dp">
    </FrameLayout>

</android.support.design.widget.CoordinatorLayout>

1 Answers1

2

Fragments don't normally intercept back/up presses. That is something that's normally done at the activity level. The reasoning behind that is because you can have multiple fragments on screen at the same time... so which one handles the back press first?

To handle it in your activity, consider keeping track of your before and after state. Let's use the fragment count as an example state:

@Override
public void onBackPressed() {
  // for every back press, if there is a fragment to remove, then remove it first
  if (getFragmentManager().getBackStackEntryCount() > 0) {
    getFragmentManager().popBackStack();
    return;
  }
  super.onBackPressed();

}

You also need to make sure you add your original fragment transaction to the backstack:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);

newsFrag1 newsFragment1 = new newsFrag1();
ft.replace(R.id.fragment_container, newsFragment1);
ft.addToBackStack();
ft.commit();

Edit: I think from your xml what you're asking for is UP navigation, not BACK navigation. If that is the case, then you still need to leverage the backstack but all the work needs to be done by the parent activity (because the activity owns the toolbar)

getSupportFragmentManager()
  .addOnBackStackChangedListener(new OnBackStackChangedListener(){
      @Override
      public void onBackStackChanged() {
        updateToolbarUp();
      }
  });

public void updateToolbarUp(){
  boolean showUp = getSupportFragmentManager().getBackStackEntryCount() > 0;
  getSupportActionBar().setDisplayHomeAsUpEnabled(showUp);
}

Then override the up press to simply pop the backstack:

@Override
public boolean onSupportNavigateUp() {
  getSupportFragmentManager().popBackStack();
  return true;
}
Jon
  • 1,715
  • 12
  • 14