0

I have a MainActivity in the onCreate method i add a fragment like this :

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, new FragmentA(), FRAGMENT_TAG).addToBackStack("main")
                .commit();

In FragmentA there is a lisview and when user clicked on an item i remove FragmentA and add FragmentB (In FragmentA there is a FragmentPagerAdapter and in FragmentPagerAdapter is a ListFragment in ListFragment i called below code )

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     getActivity().getSupportFragmentManager().beginTransaction()
                        .replace(R.id.content_frame, new FragmentB(), MainActivity.FRAGMENT_TAG).addToBackStack("main")
                        .commit();
};

Now in FragmentB there is button and whenever user clicked on it i want to go back to the FragmentA. I try to call popBackStack()in button listener but nothing happen

 FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
 fragmentManager.popBackStack();

Or i also try this below code but it gives me nullpointer exception

   FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        String fragmentTag = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
        Fragment currentFragment =getActivity().getSupportFragmentManager()
                .findFragmentByTag(fragmentTag);
        fragmentManager.beginTransaction().replace(R.id.content_frame,currentFragment).commit();
        fragmentManager.popBackStack();
Daan
  • 2,680
  • 20
  • 39
Saeed Masoumi
  • 8,746
  • 7
  • 57
  • 76
  • On what line did the nullpointer exception occur?, also calling 'replace' in the transaction does not add it to the backstack, the method 'add' however does. I did see you added it programmaticaly, but calling add should suffice – ThMBc Oct 16 '14 at 07:55
  • I got exception in >fragmentManager.beginTransaction().replace i also try add instead of replace but same exception happen – Saeed Masoumi Oct 16 '14 at 07:58
  • Try adding fragmentManager.popBackStack("main") – AmaJayJB Oct 16 '14 at 08:45
  • But it goes me to the MainActivity without any fragmens in content_frame – Saeed Masoumi Oct 16 '14 at 08:57
  • Ok, maybe try using different Tags so that when you use findFragmentByTag it wont get mixed up – AmaJayJB Oct 16 '14 at 09:25

1 Answers1

2

If you want to go to the previous Fragment in the BackStack, then all you need to do is call onBackPressed().

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 1
    Thanks it works. But i have a problem with this , if i go to the first method (before the fragmentA initializing for the first time) it show me the activity layout ,i want whenever user came to this onDestroy method called (for example if user is in fragmentB and click on back button he goes to fragmentA and when he click on back button again he goes to MainActivity , i don't want user come to the MainActivity anymore ) – Saeed Masoumi Oct 16 '14 at 09:04
  • getFragmentManager().getBackStackEntryCount() is always zero . – Saeed Masoumi Oct 16 '14 at 09:27
  • try using `.addToBackStack(null)` – EpicPandaForce Oct 16 '14 at 09:29
  • 1
    I use getSupportFragmentManager().getBackStackEntryCount() and my problem solved,Thanks – Saeed Masoumi Oct 16 '14 at 09:29