1

this is my first question.I am using below code for remove fragment:

    private boolean returnBackStackImmediate(FragmentManager fm) {

        List<Fragment> fragments = fm.getFragments();
        if (fragments != null && fragments.size() > 0) {
            for (Fragment fragment : fragments) {
                if (fragment.getFragmentManager().getBackStackEntryCount() > 0) {
                    if (fragment.getFragmentManager().popBackStackImmediate()) {
                        return true;
                    } else {
                        return returnBackStackImmediate(fragment.getChildFragmentManager());
                    }
                }
            }
        }


    return false;
}

Now,I want after remove fragment, if user click on Back button again, my activity be close.for that I have added these codes into my Activity:

    @Override
public void onBackPressed() {

    if (getSupportFragmentManager().getFragments() == null) {
        finish();
    } else {

        if (!returnBackStackImmediate(getSupportFragmentManager())) {
            super.onBackPressed();
        }
    }

}

but when I press back button and remove fragment and press again I got this error:

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.Fragment.getFragmentManager()' on a null object reference
                                                                             at com.safarayaneh.notify.activities.MainActivity.returnBackStackImmediate(MainActivity.java:501)
                                                                             at com.safarayaneh.notify.activities.MainActivity.onBackPressed(MainActivity.java:488)
                                                                             at android.app.Activity.onKeyUp(Activity.java:2477)
                                                                             at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
                                                                             at android.app.Activity.dispatchKeyEvent(Activity.java:2730)

I know fragment is null but which condition can I use for this situation?

Groot
  • 177
  • 1
  • 2
  • 10
  • dude I know what is nullPointer. but in my situation how could i solve that? – Groot Oct 30 '17 at 08:46
  • You are checking on null and that is a great start, but on the same line you are trying to get the size. I think that is the problem. Can you do the if statement for the null check separate from the size check? – H. Hakvoort Oct 30 '17 at 08:48
  • Try to check back stack fragment size inside onBackPressed() like if (getSupportFragmentManager(). getBackStackEntryCount() <= 0) {finish()} – Haresh Chhelana Oct 30 '17 at 08:49
  • @H.Hakvoort into onBackPressed method I have this statement `getSupportFragmentManager().getFragments() == null`.I've checked first but seems this statement does not work – Groot Oct 30 '17 at 08:50

3 Answers3

4

try this

@Override
public void onBackPressed() {

    int count = getSupportFragmentManager().getBackStackEntryCount();

    if (count == 0) {
        super.onBackPressed();

    } else {
         getSupportFragmentManager().popBackStackImmediate();
    }

}
0

Use getActivity().finish(); to finish Activity from fragment

getActivity().finish();
Mallikarjuna
  • 874
  • 6
  • 17
0

I am not sure why you are trying to code this all manually. If you add your fragments to your activity as follows:

  getSupportFragmentManager().beginTransaction().replace(R.id.***YOUR TARGET FRAME ID***, fragment, "some fragment tag")
                .addToBackStack(null)
                .commit();

Then the activity should handle the backstack stuff automatically. I.e. don't need to do anything with onBackPressed. When there's no more fragments in the backstack, the activity will close.

If there's a specific reason you're doing this manually let me know and I'll delete this answer (but you may need to update the question, as this simple approach does answer your question as it stands!)

Vin Norman
  • 2,749
  • 1
  • 22
  • 33