title: Pop fragment from backstack
body: <p>I am trying to create an activity which holds two fragments. One fragment is displayed when the activity starts and the other one is displayed when the user press a button in the toolbar.</p>
  
  <p>In the second fragment I want to add back navigation and when the user press the back button the second fragment should close and the first one should appear.</p>
  
  <p>But I am getting some errors and I couldn't fix them.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.geapps.notapp.activities;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v7.app.ActionBarActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.MenuItem;
  import android.view.View;
  
  import com.geapps.notapp.R;
  import com.geapps.notapp.fragments.MainFragment;
  import com.geapps.notapp.fragments.NewNoteFragment;
  
  public class MainActivity extends ActionBarActivity
  {
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if (savedInstanceState == null) 
          {
              loadFragment(new MainFragment(), "Main");
          }
  
          Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
          toolbar.setTitle("Toolbar");
          toolbar.inflateMenu(R.menu.main);
  
  
          toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() 
          {
              @Override
              public boolean onMenuItemClick(MenuItem menuItem) 
              {
                  switch (menuItem.getItemId())
                  {
                      case R.id.action_new:
                          loadFragment(new NewNoteFragment(), "New Note");
                          break;
                  }
  
                  return false;
              }
          });
      }
  
      public void loadFragment(Fragment fragment, String tag)
      {
          FragmentManager fragmentManager = getSupportFragmentManager();
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  
          fragmentTransaction.replace(R.id.main_container, fragment, tag);
          fragmentTransaction.addToBackStack(tag);
          fragmentTransaction.commit();
      }
  } 
  </code></pre>
  
  <p>MainFragment.java</p>
  
  <pre><code>package com.geapps.notapp.fragments;
  
  import java.util.ArrayList;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ListView;
  
  import com.geapps.notapp.R;
  import com.geapps.notapp.adapters.NotesListAdapter;
  import com.geapps.notapp.models.Note;
  
  public class MainFragment extends Fragment 
  {
      private View view;
  
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      {
          view = inflater.inflate(R.layout.fragment_main, container, false);
  
          loadNotes();
  
          return view;
      }
  
      private void loadNotes()
      {
          ListView notesList = (ListView)view.findViewById(R.id.notes_list);
  
          ArrayList&lt;Note&gt; notes = new ArrayList&lt;Note&gt;();
          notes.add(new Note("Sunday homework", "1. Do math homework and then sleep"));
          notes.add(new Note("Job details", "Programming job especially iOS and Android development"));
          notes.add(new Note("Journal Part 1", "What am I going to do now? I have no way to run .. :("));
  
          NotesListAdapter notesListAdapter = new NotesListAdapter(this.getActivity(), notes);
          notesList.setAdapter(notesListAdapter);
      }
  }
  </code></pre>
  
  <p>NewNoteFragment.java</p>
  
  <pre><code>package com.geapps.notapp.fragments;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v7.widget.Toolbar;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.geapps.notapp.R;
  
  public class NewNoteFragment extends Fragment 
  {
      private View view;
  
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setRetainInstance(true);
      }
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      {
          view = inflater.inflate(R.layout.fragment_new_note, container, false);
  
          Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
          toolbar.getMenu().findItem(R.id.action_new).setVisible(false);
  
          toolbar.setNavigationIcon(R.drawable.ic_action_back);
          toolbar.setNavigationOnClickListener(new View.OnClickListener() 
          {
              @Override
              public void onClick(View v) 
              {
                  FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                  fragmentManager.popBackStack("Main", FragmentManager.POP_BACK_STACK_INCLUSIVE);
              }
          });
  
          return view;
      }
  }
  </code></pre>
  
  <p>And here are the errors:</p>
  
  <pre><code>PathClassLoader(BaseDexClassLoader).findClass(String) line: 61  
  PathClassLoader(ClassLoader).loadClass(String, boolean) line: 501   
  PathClassLoader(ClassLoader).loadClass(String) line: 461    
  BackStackRecord.getEnterTransition(Fragment, boolean) line: 1056    
  BackStackRecord.configureTransitions(int, BackStackRecord$TransitionState, boolean, SparseArray, SparseArray) line: 1138    
  BackStackRecord.beginTransition(SparseArray, SparseArray, boolean) line: 1029   
  BackStackRecord.popFromBackStack(boolean, BackStackRecord$TransitionState, SparseArray, SparseArray) line: 883  
  FragmentManagerImpl.popBackStackState(Handler, String, int, int) line: 1593 
  FragmentManagerImpl$3.run() line: 509   
  FragmentManagerImpl.execPendingActions() line: 1489 
  FragmentManagerImpl$1.run() line: 454   
  Handler.handleCallback(Message) line: 615   
  FragmentActivity$1(Handler).dispatchMessage(Message) line: 92   
  Looper.loop() line: 137 
  ActivityThread.main(String[]) line: 4921    
  Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
  Method.invoke(Object, Object...) line: 511  
  ZygoteInit$MethodAndArgsCaller.run() line: 1038 
  ZygoteInit.main(String[]) line: 805 
  NativeStart.main(String[]) line: not available [native method]  
  </code></pre>