我有这样的结构:
MainActivity -> ProfileFragment -> ListFragment
其中“->”是FragmentTransaction.add(....)
我的问题是,如果我自己用实现来重新加载子片段的旧示例(例如,当旋转设备时),我需要android吗?
因为当android重新创建第一个Activity并到达onStart()
方法时,Activity本身会重新创建ProfileFragment等等。
我也尝试了一些测试Log
和结果如下
第一次创造:
2023-11-02 18:54:59.141 16908-16908 Rotation Test it.uniba.dib.sms222334 D ProfileFragment instance created from MainActivity
2023-11-02 18:54:59.161 16908-16908 Rotation Test it.uniba.dib.sms222334 D ProfileFragment: ProfileFragment{4eef489} (7ad3c3ad-8de4-4205-bc33-5e803c9c0f36 id=0x7f090101) onCreateView()
2023-11-02 18:54:59.170 16908-16908 Rotation Test it.uniba.dib.sms222334 D ListFragment instance created from ProfileFragment
2023-11-02 18:54:59.171 16908-16908 Rotation Test it.uniba.dib.sms222334 D ListFragment: ListFragment{74e1ea1} (03980350-9ab1-4857-a7f3-d71560f949cf id=0x7f0901bb) onCreateView()
字符串
旋转后:
2023-11-02 18:51:46.127 16908-16908 Rotation Test it.uniba.dib.sms222334 D ProfileFragment instance created from android
2023-11-02 18:51:46.128 16908-16908 Rotation Test it.uniba.dib.sms222334 D ListFragment instance created from android
2023-11-02 18:51:46.164 16908-16908 Rotation Test it.uniba.dib.sms222334 D ProfileFragment instance created from MainActivity
2023-11-02 18:51:46.167 16908-16908 Rotation Test it.uniba.dib.sms222334 D ProfileFragment: ProfileFragment{9373e8c} (a5b6a134-359d-46bc-a487-270f4e970b41 id=0x7f090101) onCreateView()
2023-11-02 18:51:46.410 16908-16908 Rotation Test it.uniba.dib.sms222334 D ListFragment: ListFragment{fb9c6ab} (a78556b1-fe09-4b21-b40b-c3fc21fc9728 id=0x7f0901bb) onCreateView()
2023-11-02 18:51:46.416 16908-16908 Rotation Test it.uniba.dib.sms222334 D ProfileFragment: ProfileFragment{5d59c38} (063fb353-0129-4a4a-9db3-c72578aea443 id=0x7f090101) onCreateView()
2023-11-02 18:51:46.427 16908-16908 Rotation Test it.uniba.dib.sms222334 D ListFragment instance created from ProfileFragment
2023-11-02 18:51:46.428 16908-16908 Rotation Test it.uniba.dib.sms222334 D ListFragment: ListFragment{aad4157} (23af9cd1-15e2-4ddc-aa59-c138b79a5d8e id=0x7f0901bb) onCreateView()
型
为了完整起见,我也把这种情况下的构造函数
ListFragment:
public ListFragment() {
Log.d("Rotation Test","ListFragment instance created from android");
currentUser=SessionManager.getInstance().getCurrentUser();
currentUserRole=currentUser.getRole();
}
public ListFragment(String overrideParams){
}
public static ListFragment newInstanceProfile(ProfileFragment.Tab tabPosition, ProfileFragment.Type profileType, User profile) {
ListFragment myFragment = new ListFragment("justForOverrideTheConstructor");
Bundle args = new Bundle();
args.putInt("tab_position", tabPosition.tabPosition.ordinal());
args.putInt("profile_type", profileType.ordinal());
args.putParcelable("profileData", profile);
myFragment.setArguments(args);
Log.d("Rotation Test","ListFragment instance created from ProfileFragment");
return myFragment;
}
型
ProfileFragment:
public ProfileFragment(){
Log.d("Rotation Test","ProfileFragment instance created from android");
}
private ProfileFragment(String overrideParams){
}
public static ProfileFragment newInstance(User profile) {
ProfileFragment myFragment = new ProfileFragment("justForOverrideTheConstructor");
Bundle args = new Bundle();
args.putParcelable("profileData", profile);
myFragment.setArguments(args);
Log.d("Rotation Test","ProfileFragment instance created from MainActivity");
return myFragment;
}
型
所以当我旋转设备时,你可以看到,我有两个片段的两个示例,这不是我想要的结果。
我看到一些关于setRetainInstance(true)
的解决方案,但它是一个很好的解决方案?我知道它是与非UI片段一起使用的,否则它会产生内存泄漏。
1条答案
按热度按时间o3imoua41#
你可以用这两个回调函数来处理,
字符串
因此,您不会使您的片段不可销毁(恕我直言,这是不可能的,因为您的活动会重新创建),但您可以保存它们所具有的UI状态并正确地恢复它