0
votes

I have an activity with 4 fragments. I would like to avoid situations where user click 100 times before exit. When a fragment is set, it's added to back stack. For example: User sets fragments in this order: 1, 2, 3, 4 then he sets frag 2 and then 4. My backStack looks like that (fragment 4 is visible): 2, 4, 3, 2, 1 and exit. I would like that my backStack looks like: 2, 3, 1 and exit.

I've tried to solve my problem with popBackStackImmediate(string, int flags), like in this post: Prevent The Same Fragment From Stacking More Than Once ( addToBackStack) But my backStack looks like (with 4 visible) 2, 1 and exit. I deduced that popBackStackImmediate() is removing stacks on top of the stack which is popped (I hope you know what I mean).

private void setFragment(Fragment fragment) {

        String backStateName = fragment.getClass().getName();

        FragmentManager manager = getSupportFragmentManager();
        boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);

        if (!fragmentPopped){ //fragment not in back stack, create it.
            FragmentTransaction ft = manager.beginTransaction();
            ft.replace(R.id.main_frame, fragment);
            ft.addToBackStack(backStateName);
            mMainNav.setItemBackgroundResource(R.color.PopWindowBorder_HomeRenovationfragment);
            ft.commit();
        }
    }

I expect to have each fragments no more than one time in the back stack without disrupting the order of the backStack in order to have a back stack which looks like that: 2, 3, 1 and exit. :)

1
Have you considered blocking user from actually adding fragment more then once? That sounds more natural to me.ror
have you considered using a viewpager with a fragment adapter ?a_local_nobody
@a_local_nobody it's sounds good to me too. But it isn't possible, if you block him to add a second time, it won't do the right thing. In fact I need to remove the old stack and popBackStack works wonderfully, but is supress other stacks... Whhen blocking him, I'll obtain this back stack (with frag 4 visible): 4, 3, 2, 1 and exit. I think it will throw an exeption but anyway, I don't expect this result :)mathislr
@a_local_nobody what is a viewPager with a fragment adapter (sorry, I'm a beginner ^^). Do you have a link that could enlighted me?mathislr
no problem, a viewpager with a fragment adapter is commonly used when you want to navigate between different fragments, have a look at this : developer.android.com/training/animation/screen-slide. Note that the swiping part is only part of the benefita_local_nobody

1 Answers

0
votes

Please find the below code, as it can help you, I can add it in comment section also but my reputation was not enough to add comments

 private fun loadFragment(fragment: Fragment, previousFragment: Fragment?) {

    val transaction = supportFragmentManager.beginTransaction()
 if (supportFragmentManager.findFragmentByTag(fragment::class.java.simpleName) != null) {
        if (previousFragment != null) {
            transaction.hide(previousFragment).show(fragment).commit()
        } else {
            transaction.show(fragment).commit()
        }
    } else {

        if (previousFragment != null) {
            transaction.hide(previousFragment).add(R.id.frame_container, fragment, fragment::class.java.simpleName).addToBackStack(null).commit()
        } else {
            transaction.add(R.id.frame_container, fragment, fragment::class.java.simpleName).addToBackStack(null).commit()
         }
    }
    [email protected] = fragment
  }