0

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. :)

mathislr
  • 3
  • 3
  • Have you considered blocking user from actually adding fragment more then once? That sounds more natural to me. – ror Jul 22 '19 at 09:25
  • have you considered using a viewpager with a fragment adapter ? – a_local_nobody Jul 22 '19 at 09:37
  • @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 Jul 22 '19 at 09:55
  • @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 Jul 22 '19 at 09:57
  • no problem, a viewpager with a fragment adapter is commonly used when you want to navigate between different fragments, have a look at this : https://developer.android.com/training/animation/screen-slide. Note that the swiping part is only part of the benefit – a_local_nobody Jul 22 '19 at 09:59
  • @a_local_nobody I've just read this article (thank you by the way) but I don't really understand how it could help me, could you explain me please? (I said that I was a beginner ;) ). – mathislr Jul 22 '19 at 13:43
  • no problem, so one way of handling fragments and navigation is to use a viewpager (it allows you to swipe between fragments, but you can disable this if you want). Then, you give your viewpager an adapter of fragments. This adapter of fragments only contains ONE instance of each fragment, then you can tell the viewpager to go to a certain page, depending on the user's actions and it will navigate to that fragment instance. – a_local_nobody Jul 22 '19 at 13:46
  • Oh, it sounds to be very usefull in my case, I'm gonna read some documents and tutorial ans rework my code, thank you very much!! – mathislr Jul 22 '19 at 13:49
  • no problem, i'll try give you some code in an hour or two when i get home :P – a_local_nobody Jul 22 '19 at 13:52
  • @a_local_nobody I was wondering, a Viewpager can combine with a bottomNavigationView? – mathislr Jul 22 '19 at 13:53
  • yes, they work fine together – a_local_nobody Jul 22 '19 at 13:53
  • it's just going to make life a bit more complicated for you though, you will have to manually handle navigation, but it does work – a_local_nobody Jul 22 '19 at 13:54
  • @a_local_nobody I thank you a lot for your patience and explanations. I've learned quite an important aspect of android programation thanks to you. Your solution could be great but I think it will take me a lot of time. Do you know if it is possible just to modify the default behavior of popBackStackImmediate(string, int flags) to avoid the removal of stacks in the backStack? – mathislr Jul 22 '19 at 14:03
  • @a_local_nobody I've made the changes in order to implement a viewPager but now I don't know how to add fragments to back stack because no more transaction is comitted. – mathislr Jul 22 '19 at 18:26
  • for today, i'm done helping, i need some sleep, i'll get back to you tomorrow, try having a look at this https://stackoverflow.com/questions/14368242/how-to-setcurrentpage-in-viewpager-according-to-user-selection-how-do-i-know-w – a_local_nobody Jul 22 '19 at 18:29
  • Yeah, it's no problem :), thanks for the link anyway* – mathislr Jul 22 '19 at 18:51

1 Answers1

0

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()
         }
    }
    this@BaseActivity.previousFragment = fragment
  }
Mohan Sai Manthri
  • 2,808
  • 1
  • 11
  • 26
  • hi, thanks for your answer but it doesn't delete the fragment in backStack ^^. Thank you anyway – mathislr Jul 22 '19 at 13:31
  • Or maybe I haven't understood your code because it's written in Kotlin and I don't know this language ^^. – mathislr Jul 22 '19 at 13:36
  • Hey @mathislr, In above code we'll only add the fragment if it was not present in the back stack or else we will just hide the previous fragment & show the current fragment, It was tested and working code, I've used this in so many projects, If there is any issue please drop it in comment section, I'll definitely to try to resolve the issue as soon as possible. – Mohan Sai Manthri Jul 24 '19 at 10:21
  • This code is written in kotlin, isn't it? Because I don't know what val and fun are... I'm personnaly codding in java, thank you anyway for according me time :) – mathislr Jul 24 '19 at 13:31