title: Display and use Back button in fragment
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public class NewFrag extends Fragment{
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
          setHasOptionsMenu(true);
  
  
      }
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.newfrag, container, false);
  
      }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>public class PrimaryFragment extends Fragment {
  
      Button btn1;
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.primary, container, false);
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          btn1 = (Button)view.findViewById(R.id.btn1);
          btn1.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  Fragment var1 = new NewFrag();
                  ((MainActivity)getActivity()).replaceFragment(var1);
  
              }
          });
      }
  
  
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>public class TabFragment extends Fragment {
  
      public static TabLayout tabLayout;
      public static ViewPager viewPager;
      public static int int_items = 2 ;
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.tab_layout, container, false);
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          tabLayout = (TabLayout) view.findViewById(R.id.tabs);
          viewPager = (ViewPager) view.findViewById(R.id.viewpager);
  
          viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
  
          tabLayout.post(new Runnable() {
              @Override
              public void run() {
                  tabLayout.setupWithViewPager(viewPager);
              }
          });
      }
  
      class MyAdapter extends FragmentPagerAdapter {
  
          public MyAdapter(FragmentManager fm) {
              super(fm);
          }
  
          /**
           * Return fragment with respect to Position .
           */
  
          @Override
          public Fragment getItem(int position)
          {
              switch (position){
                  case 0 : return new PrimaryFragment();
                  case 1 : return new SocialFragment();
              }
              return null;
          }
  
          @Override
          public int getCount() {
  
              return int_items;
  
          }
  
          /**
           * This method returns the title of the tab according to the position.
           */
  
          @Override
          public CharSequence getPageTitle(int position) {
  
              switch (position){
                  case 0 :
                      return "Primary";
                  case 1 :
                      return "Social";
              }
              return null;
          }
      }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public class MainActivity extends AppCompatActivity{
      DrawerLayout mDrawerLayout;
      NavigationView mNavigationView;
      ActionBarDrawerToggle mDrawerToggle;
      FragmentManager mFragmentManager;
      FragmentTransaction mFragmentTransaction;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          /**
           *Setup the DrawerLayout and NavigationView
           */
  
          mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
          mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
  
          /**
           * Lets inflate the very first fragment
           * Here , we are inflating the TabFragment as the first Fragment
           */
  
          mFragmentManager = getSupportFragmentManager();
          mFragmentTransaction = mFragmentManager.beginTransaction();
          mFragmentTransaction.replace(R.id.containerView,new TabFragment()).addToBackStack(null).commit();
          /**
           * Setup click events on the Navigation View Items.
           */
  
          mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
              @Override
              public boolean onNavigationItemSelected(MenuItem menuItem) {
                  mDrawerLayout.closeDrawers();
  
  
  
                  if (menuItem.getItemId() == R.id.nav_item_inbox) {
                      FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                      fragmentTransaction.replace(R.id.containerView,new NavOne()).addToBackStack(null).commit();
  
                  }
  
                  if (menuItem.getItemId() == R.id.nav_item_sent) {
                      FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                      xfragmentTransaction.replace(R.id.containerView,new NavTwo()).addToBackStack(null).commit();
                  }
  
                  return false;
              }
  
          });
  
          /**
           * Setup Drawer Toggle of the Toolbar
           */
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
                  R.string.app_name);
  
          mDrawerLayout.setDrawerListener(mDrawerToggle);
  
          mDrawerToggle.syncState();
  
  
  
      }
  
      public void replaceFragment(Fragment fragment){
          FragmentTransaction t = getSupportFragmentManager().beginTransaction();
          t.replace(R.id.containerView, fragment).addToBackStack(null);
          t.commit();
      }
  
      public void showArrow(){
          mDrawerToggle.setDrawerIndicatorEnabled(false);
          //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      }
  
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
  
          switch (item.getItemId()) {
              case android.R.id.home:
                  onBackPressed();
                  break;
              default:
                  break;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
  
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>This topic may look very old and there might be many answers but my situation is very unique. I have to implement Navigation Drawer and TabLayout in MainActivity. So I used this <a href="https://androidbelieve.com/navigation-drawer-with-swipe-tabs-using-design-support-library/" rel="nofollow">tutorial</a> for that. It uses FrameLayout + Navigation in MainActivity and attaches a fragment with the FrameLayout. This fragment has TabLayout. Now I have a button in one of the Tab's fragment which will open another fragment. I want to display <code>back arrow</code> button in this new fragment. I disabled drawertoggle but unable to show back button. But my app crashes with a NullPointerException. Is there any other way to show the arrow button and also use it to go to previous fragment?</p>