1

I am making an app with a couple of screens and it has to be possible to navigate through the app with the back button.

I am having trouble with one screen, my MainFragment. It doesn't seem recognise the backstack.

The MainFragment is started from the MainActivity here:

if (savedInstanceState == null) {
        getFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).addToBackStack("mainFragment")
                .commit();
    }

So when the app starts, this fragment is loaded. From this mainFragment I can navigate to different screens. I have methods in my mainActivity which I call in my MainFragment to navigate:

public void navigateToListCountriesSeen(){
    getFragmentManager().beginTransaction().replace(R.id.container, new listCountriesSeenFragment()).addToBackStack("listSeen")
            .commit();
}

public void navigateToListCountriesToSee(){
    getFragmentManager().beginTransaction().replace(R.id.container, new listCountriesToSeeFragment()).addToBackStack("listToSee")
            .commit();
}

But whenever I am in the countriesSeen or CountriesToSee fragments/screens and I press the back button, the app just closes...

How do I solve that?

FerialTeut
  • 126
  • 9

2 Answers2

0

One possible solution is to check the fragment in onBackPressed:

public void onBackPressed() {

    Fragment currentFragment =getFragmentManager().findFragmentById(R.id.ll_fragment_container);

    if(currentFragment instanceof CountriesToSee && currentFragment.isVisible()){ //if fragment for info is showing close fragment when back pressed. Otherwise close app
        removeFragment(currentFragment);

    }else{
        super.onBackPressed();
    }

}
Jaz
  • 371
  • 1
  • 6
  • 20
  • This looks awesome!! How do I write the removeFragment method? – FerialTeut Feb 19 '16 at 19:38
  • public void onBackPressed() { Fragment currentFragment = getFragmentManager().findFragmentByTag("listSeen"); if(currentFragment instanceof listCountriesSeenFragment && currentFragment.isVisible()){ //if fragment for info is showing close fragment when back pressed. Otherwise close app getFragmentManager().beginTransaction().remove(currentFragment).commit(); }else{ super.onBackPressed(); } } I tried this, but then there is just nothing? The Mainfragment doesnt appear? – FerialTeut Feb 19 '16 at 19:53
  • If you are looking to replace listCountriesSeenFragment with the MainFragment you could just use fragmenttransaction.replace(R.id.fragment_container, newFragment); – Jaz Feb 19 '16 at 23:02
  • It would be better to get addtobackstack working correctly however. Were you already overriding onbackPressed in the Activity? – Jaz Feb 19 '16 at 23:09
  • No, do I need to override the onBackPressed method to make addtobackstack properly? – FerialTeut Feb 21 '16 at 01:57
  • It's the better method. But the above will probably work as well using the replace method. – Jaz Feb 22 '16 at 20:35
0

Use addToBackStack() if you want the navigation to backup on the backspace key. Here's an example:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
ListCountriesSeenFragment listCountriesSeenFragment = new ListCountriesSeenFragment();
fragmentTransaction.replace(R.id.frameLayout, listCountriesSeenFragment, FRAGMENT_TAG_COUNTRIES_SEEN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
RickBoyerDev
  • 2,081
  • 1
  • 18
  • 15
  • But I am using addToBackStack? Scroll to the right in my code segments – FerialTeut Feb 19 '16 at 19:25
  • Sorry, you're right, I didn't see them. Which class are you extending for your activity? I used `getSupportFragmentManager()` in the code above because I'm extending `AppCompatActivity`. – RickBoyerDev Feb 19 '16 at 19:29
  • I am extending AppCombatActivity too, is there a difference between getSupportFragmentManager and getFragmentManager? – FerialTeut Feb 19 '16 at 19:35
  • Yes, you don't want to mix Support calls with native SDK calls. Try using the `getSupportFragmentManager()` as I use above. – RickBoyerDev Feb 19 '16 at 19:58