注册 | 登录

android - Fragments within a Fragment cannot retain their values

itPublisher 分享于



推荐:android Fragments:管理fragment

要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()。 你可以用FragmentManager来做以上事情:   1使用方法findF

I have an activity and in that activity I have 2 Fragments A and B.

Fragment A has a EditText for entering Email.

Fragment B contains 2 more fragments B1 and B2 which can be selected using two buttons at the bottom of Fragment B.

B1 and B2 contain an EditText for entering Name and Number.

When i enter Email, Name and Number and switch between fragments A and B, Email value retains BUT the Name and Number values in sub-fragments B1 and B2 gets lost.

Is it because i have nested fragments in B?

How can i retain the values of sub-fragments B1 and B2?

推荐:android Fragments详解二:创建Fragment . 创建Fragment     要创建fragment,必须从Fragment或Fragment的派生类派生出一个类。Fragment的代码写起

I am new to android, Can anybody refer a good working example?

android android-activity android-fragments android-savedstate
  this question
asked Mar 11 '15 at 13:10 Kazmi 218 1 3 15      This might be relevant:… –  mach Mar 11 '15 at 13:14


1 Answers

It's always a pain working with 2+ fragments.

Not the best workaround: Try using SharedPreferences to save and display Name and Number, and reload the entire activity when needed so that changes get effected.

  this answer
answered Mar 11 '15 at 13:33 Sajith Sageer 56 6      As i am new with android so I was testing it with the text fields .... In reality i will displaying 2 different ListViews in Fragments A and B. And i don't want my list views to loose the values. –  Kazmi Mar 16 '15 at 14:05      Are you using tabs to switch between fragments? –  Sajith Sageer Mar 16 '15 at 17:50      I am selecting Fragment A and B from a menu which slides in from left, and Fragment B has two button at the bottom with which you can switch between sub-fragments B1 and B2. –  Kazmi Mar 19 '15 at 10:03      i have found 2 solutions but don't know which one is efficient so would like your suggestions, please see the link, i appreciate your help so far.… –  Kazmi Mar 26 '15 at 10:10      In my project I used 'replace' a lot, to change fragments and ended up having many problems. So I would say go with the first solution. –  Sajith Sageer Mar 28 '15 at 9:59  |  show more comment

推荐:android Fragments详解四:管理fragment

要管理fragment们,需使用FragmentManager,要获取它,需在activity中调用方法getFragmentManager()。 你可以用FragmentManager来做以上事情: 1使用方法findFra








您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱