How to add BackStackEntry to BackStack if not exists
Asked Answered
R

2

8

I've added custom keyboard to my Fragment and now I want to implement closing keyboard when on back pressed.

class CustomKeyboard
{
    public void init(Context context) {
        //...

        FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
        boolean fragmentPopped = fragmentManager.popBackStackImmediate(TAG, 0);
        if (!fragmentPopped) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.addToBackStack(TAG);
            fragmentTransaction.commit();
        }
    }
}

The problem is init calls every time when screen rotated because I create CustomKeyboard in public void onActivityCreated(final Bundle savedInstanceState)

fragmentPopped=false every time, so CustomKeyboard may be added to BackStack more than one time.

My question:
Is it possible to add BackStackEntry to BackStack if not exists without using getBackStackEntryCount() method?

Renault answered 25/9, 2016 at 19:18 Comment(5)
developer.android.com/reference/android/app/…Danby
you want your question answered directly or want to solve your problem of multiple calls?Morentz
@Morentz I want my question answered directlyRenault
I believe there is a bug somewhere in your code. You should post some more code.Frail
You want to say that you have to check backstack entry with some other method other than getBackStackEntryCount() and if it is 0 then add fragment Right ?Stroman
B
2

Try the below updated code.

public void init(Context context) { 

    FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
    boolean fragmentPopped = fragmentManager.popBackStack(TAG, 0);
    if (!fragmentPopped) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.addToBackStack(TAG);
        fragmentTransaction.commit();
    }
}
Boatel answered 7/10, 2016 at 6:12 Comment(0)
S
2

You can do something like this :

     FragmentManager fm= getSupportFragmentManager();

    if(fm!=null && getSupportFragmentManager().getFragments()!=null) {

        boolean fragmentPopped = fm.popBackStackImmediate(TAG, 0);

        if (!fragmentPopped && getSupportFragmentManager().getFragments().size() == 0) {

            FragmentTransaction ft = fm.beginTransaction();
            ft.addToBackStack(TAG);
            ft.commit();
        }
    }

Also You can clean your Backstack first like this and then replace your fragment :

  private void cleanBackStack() {
    FragmentManager fm = getSupportFragmentManager();
    if (fm != null && fm.getFragments() != null) {
        if (fm.getFragments().size() > 1) {

            for (int entry = 0; entry < fm.getFragments().size(); entry++) {
                fm.popBackStackImmediate();
            }
        } else if (fm.getFragments().size()==1){

            fm.popBackStack();
        }else if(fm.getFragments().size()==0){
              //REPLACE YOUR FRAGMENT
        }
    }
}
Stroman answered 7/10, 2016 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.