android的Fragment切换时如何保存状态?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android的Fragment切换时如何保存状态?相关的知识,希望对你有一定的参考价值。
android的Fragment,使用transaction.replace(R.id.main_container_bottom, fragment);transaction.addToBackStack(null); transaction.commit();可以把一个fragment保存到返回栈中,但是按返回键返回时那个fragment会再次调用onCreateView方法,而该方法又是通过inflater.inflate(R.layout.test, container, false);设置界面布局的地方,再次调用相当于刷新了界面,那还怎么保存之前的状态?请问正确的保存及恢复前一个fragment的方法
自己在网上找的,我自己的问题解决了,你的能不能解决我不是很清楚,可以参考一下@Override
public void onHiddenChanged(boolean hidden)
// TODO Auto-generated method stub
super.onHiddenChanged(hidden);
if(hidden)
//在这调数据库保存信息
else
//在这从数据库读取信息
参考技术A 没有,正好遇到这个问题,上网一查有同样疑问的也不少,但是貌似没有很好的解决,所以拿过来自己发帖,希望高手指教一下。 参考技术B 眼前一亮,不错的帖子哦
Fragment切换返回
1.开发中遇到需要在Fragment之间切换,保存状态
(1).初始化Fragment:
public void initFragment(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl_container,onCreteFragment());
ft.commitAllowingStateLoss();//允许丢失,一般可以不用这个采用commit()替代
}
5
5
1
public void initFragment(){
2
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
3
ft.replace(R.id.fl_container,onCreteFragment());
4
ft.commitAllowingStateLoss();//允许丢失,一般可以不用这个采用commit()替代
5
}
(2).切换Fragment算法:
public void switchFragment(Fragment from, Fragment to) {
FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
if (!to.isAdded()) { // 先判断是否被add过
mFragmentTransaction.hide(from).add(R.id.fl_container, to)
.commit(); // 隐藏当前的fragment,add下一个到Activity中
} else {
mFragmentTransaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
}
}
9
9
1
public void switchFragment(Fragment from, Fragment to) {
2
FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
3
if (!to.isAdded()) { // 先判断是否被add过
4
mFragmentTransaction.hide(from).add(R.id.fl_container, to)
5
.commit(); // 隐藏当前的fragment,add下一个到Activity中
6
} else {
7
mFragmentTransaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
8
}
9
}
(3).封装返回监听,采用弱引用和静态内部类,避免内存泄漏
public static class MyOnBackListener implements BaseFragment.OnBackListener {
private final WeakReference<BaseFragment> mBaseFragment;//目的Fragment
private final WeakReference<BaseFragmentActivity> mBaseActivity;
/**
* @param toFragment 跳转目的Fragment
* @param baseActivit 当前BaseActivity
*/
public MyOnBackListener(BaseFragment toFragment,BaseFragmentActivity baseActivit) {
mBaseFragment =new WeakReference<>(toFragment);
mBaseActivity =new WeakReference<>(baseActivit);
}
@Override
public void back(BaseFragment baseFragment) {
BaseFragmentActivity baseActivity = mBaseActivity.get();
BaseFragment baseFragment2 = mBaseFragment.get();
if (baseActivity !=null&& baseFragment2 !=null&&baseFragment!=null){
baseActivity.switchFragment(baseFragment,baseFragment2);
}
}
}
21
21
1
public static class MyOnBackListener implements BaseFragment.OnBackListener {
2
private final WeakReference<BaseFragment> mBaseFragment;//目的Fragment
3
private final WeakReference<BaseFragmentActivity> mBaseActivity;
4
/**
5
* @param toFragment 跳转目的Fragment
6
* @param baseActivit 当前BaseActivity
7
*/
8
public MyOnBackListener(BaseFragment toFragment,BaseFragmentActivity baseActivit) {
9
mBaseFragment =new WeakReference<>(toFragment);
10
mBaseActivity =new WeakReference<>(baseActivit);
11
}
12
13
14
public void back(BaseFragment baseFragment) {
15
BaseFragmentActivity baseActivity = mBaseActivity.get();
16
BaseFragment baseFragment2 = mBaseFragment.get();
17
if (baseActivity !=null&& baseFragment2 !=null&&baseFragment!=null){
18
baseActivity.switchFragment(baseFragment,baseFragment2);
19
}
20
}
21
}
(4).Activity中简单使用:
private void initLocalFragment() {
mRfbf = new RecyclerFabBehaviorFragment();
mRfbf.setOnBackListener(new MyOnBackListener(mStartRecycFragment,this));
}
4
4
1
private void initLocalFragment() {
2
mRfbf = new RecyclerFabBehaviorFragment();
3
mRfbf.setOnBackListener(new MyOnBackListener(mStartRecycFragment,this));
4
}
@Override
public void click(View view, int position) {
switch (position){
case 0:
switchFragment(mStartRecycFragment, mRfbf);
break;
}
}
8
8
1
2
public void click(View view, int position) {
3
switch (position){
4
case 0:
5
switchFragment(mStartRecycFragment, mRfbf);
6
break;
7
}
8
}
(5).子类Fragment中记得调用back:eg
TabyoutViewPagerFragment extends BaseFragment
private void initToolbar() {
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnBackListener!=null){
mOnBackListener.back(TabyoutViewPagerFragment.this);
}
}
});
}
10
10
1
private void initToolbar() {
2
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
3
4
public void onClick(View v) {
5
if (mOnBackListener!=null){
6
mOnBackListener.back(TabyoutViewPagerFragment.this);
7
}
8
}
9
});
10
}
(6).可以采用鸿阳的FABridge来简化Fragment中定义接口在Activity中回调
annotationProcessor ‘com.zhy.fabridge:fabridge-compiler:1.0.0‘
implementation ‘com.zhy.fabridge:fabridge-api:1.0.0‘
2
2
1
annotationProcessor ‘com.zhy.fabridge:fabridge-compiler:1.0.0‘
2
implementation ‘com.zhy.fabridge:fabridge-api:1.0.0‘
Fragment中定义
Fabridge.call(getActivity(), EVENT_CLICK, "来了");
1
1
1
Fabridge.call(getActivity(), EVENT_CLICK, "来了");
Activity中使用
@FCallbackId(id = FabrigeSampleFragment.EVENT_CLICK)
public void menuItemClick(String title)
{
Toast.makeText(this, ""+title+","+Thread.currentThread().getName(), Toast.LENGTH_SHORT).show();
}
x
1
id = FabrigeSampleFragment.EVENT_CLICK) (
2
public void menuItemClick(String title)
3
{
4
Toast.makeText(this, ""+title+","+Thread.currentThread().getName(), Toast.LENGTH_SHORT).show();
5
}
以上是关于android的Fragment切换时如何保存状态?的主要内容,如果未能解决你的问题,请参考以下文章
Android开发实战篇 ViewPager中切换及滑动切换Fragment数据保存状态
Android Fragment 在按下后退按钮时未保存 UI 状态
Android Fragment使用 Activity, Fragment, WebView的状态保存和恢复