Monday, September 30, 2013

Back stack with nested fragments + back button

When you have fragments with nested fragments, the FragmentActivity which handles the BACK button will not properly handle the back stack of those nested fragments.

In my application, I'm using a ViewPager with some fragments, and only one of them has nested fragments added to the back stack. In this case, when someone tries to press the back button, the activity will not handle the nested fragment stack and will finish right away. To solve this problem, I've implemented the following hack to the FragmentActivity. Please note that the activity only has one back stack: this will not work properly when you have a ViewPager with multiple fragments adding/replacing fragments into the back stack as it will get messy when the stack is not in a desired order.


asdfasdf said...

Hello, great hack. I'm interested to know more about this as I failed to make mine work. Could you share the full code of this snippet?


joão said...

This code goes into the Activity that holds a fragment with multiple child fragments. So this is practically the full code.

It was working very well when our code base was still very small. But then, after some time, this added unnecessary complexity to our code base when it was getting larger and more fragments were trying to add children. As I wrote in a comment in the code snipped, it just doesn't work at such condition.

So to take all this complexity out of our code, we decided to change our design to not use Fragments at all. We're currently using an approach very similar to what Square's "flow" library offers. Take a look at a sample they provided on this blog post to know a bit more. This helped us splitting our ui code into smaller components eventually making our code much more easy to maintain and to test as well.

asdfasdf said...

Okay, I understand. I'll check Flow out. Thanks!

Anonymous said...

hi, can yo please clarity whether
Are you using just a single Frame layout to replace various fragments?

Krystal said...

Blogcothebanchuabiet - Nơi tổng hợp những điều thú vị trong cuộc sống như những địa điểm chơi Noel ở TPHCM 2015 rực rỡ nhất hay bạn nên biết nguồn gốc và ý nghĩa của lễ giáng sinh hay tặng cho nhau những lời chúc giáng sinh hay và ý nghĩa nhất 2015 cho bạn bè, người thân hay bí quyết cách quên người yêu cũ nhanh nhat hiệu quả hay top 10 điện thoại đắt nhất thế giới bạn sẽ bất ngờ hay nguồn gốc thứ 6 ngày 13 và những sự kiện liên quan hay tổng hợp những hình xăm nhỏ đẹp hay ý nghĩa 2015 hay hướng dẫn cách làm cánh gà tẩm bột chiên xù cực ngon hay mách bạn cách trị thâm quầng mắt hiệu quả nhất hay bạn muốn câu cá thì cách làm mồi câu cá rô phi sông hiệu quả hay tổng hợp những câu đố vui hay nhất có đáp án hay nhất hay bí quyết giảm mỡ bụng sau sinh bằng rượu gừng nghệ cực hiệu quả hay mách mẹ những thực phẩm giàu axit folic tốt cho mẹ và bé hay cách chữa hóc xương cá hiệu quả nhất hay chia sẻ bạn những điều không nên chia sẻ trên Facebook cực nguy hiểm hay hiện tượng nháy mắt trái có điềm gì mang đến xui xẻo gì không hay tổng hợp những món ăn kỵ nhau gây chết người bạn nên biết hay những cách gặp ma đầy thú vị hay những điều kiêng kỵ khi hôn đàn ông nên biết hay giải mã giấc mơ thấy rắn là điềm gì xui hay không hay bà bầu nên ăn gì trong 3 tháng đầu thai kỳ hay uống nước thì cách uống nước đúng cách hay cây chùm ngây và tác dụng của cây chùm ngây thần kỳ.


This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future... TutuApp iOS 11

CNG said...

I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer.Thanks. Cydia iOS 11.3

Katy said...

I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! Oceanhorn 2 Apk Data

Anonymous said...

NoBet: NoBet Bonus Code For Nigeria | Bet with BONUS
Latest nobet ラッキーニッキー bonus codes and no deposit bonuses in Nigeria. Get the latest no bet offers for Nigeria, the top mobile betting 다파벳 sites and get

debraplm51 said...

that's really good post

bhanusuru597 said...

I implemented this exact functionality in my app for around a year. One activity with 4 tabs. Each tab is a parent fragment with it's own stack of child fragments. So for example you can be on tab A and navigate 3 levels deep, then switch to Tab B and navigate 4 levels deep, then switch back to Tab A and you will be on level 3 still with the ability to navigate backwards.

Essentially what you want to do is have a reference to the current active fragment and use popBackStack on the childFragmentManager, something like:

int backstackCount = currentFragment.getChildFragmentManager().getBackStackEntryCount();
if (backstackCount > 0) {
with all that said, I highly recommend against this approach. Instead consider using just parent fragments (or even better remove all the fragments and just use views) and keep track of your own navigation stack and implement you own navigation controller. It's a bit more work to get set up but will save you a ton of time in the futur