user559730
user559730

Reputation:

How propely work with nested fragments?

I have an application with one activity and many fragments that work on tablets with Android 4.0 and user support.library.v4

The first screen shows two fragments A and B:

----------------------------------
A      | B   <view pager>
       |------------------------
<list  |
view>  |
       |     <view pager>
       |
----------------------------------

After click on A item I add a new fragment A with add to back stask for current fragment A. I show a new fragments A1, C - C includes nested fragments D and E are added via replace(, D, ) without adding to backstak

----------------------------------
A1     |             C
       |
<list  |    D      |     E
view>  |           |
       |           |
       |           |
----------------------------------

The issue occurs when I start working on back behaviour. I start use getShildFragmentManager() for my adapter in fragment B so back start show a previous screen but after multiple clicks between this fragments: A -> A1, C(D,E) -> BACK -> A -> A1, C(D,E) -> BACK -> A -> A1, C(D,E) -> BACK -> [ISSUE] I see a fragment A but don't see a fragment B sometimes is just background from previous fragment D and E

Could you tell me where is my issue?

ADDITIONAL INFO 1) I set layout with two framelayout-containers for fragments A and B 2) A is list fragment 3) user click on item of A 4) A is replaced by new A instance and A is added in backstak; B is replaced by new fragment C that has layout with two framelayout containers for D and E fragments 5) User click on A instance and C shows D and E fragments via repalce and don't add this transaction into backstack 6) User click BACK 7) return to step 1)

Upvotes: 2

Views: 446

Answers (1)

user559730
user559730

Reputation:

So after some time I found solution:

  1. Use ChildFragmentManger when you work with fragment nested in others fragments
  2. Don't use any fragment transaction after onPause() (you can use commitStateLoss(), but it is not resolve all issues and it is discourage by several reasons)
  3. Keep track of your childs fragments and don't forget to remove them from backstack or reuse

Upvotes: 1

Related Questions