Activity嵌Fragment,如何切换时保存当前数据

使用这种用法先熟悉Fragment的生命周期,如下图所示,所以在一个Activity中切换Fragment时,会有数据被清空的情况,下面大致讲下如何保存当前页面数据,不用再次请求网络


Activity切换Fragment的方法如下:注意加粗代码,否则Fragment的getArguments会报null异常

switch (arg0.getId()) {
		case R.id.home_btu_icon01:
			CommonTools.applyRotation(home_btu_icon01, 0, 360);

			setbottomimg(1);
			
			if (null == Fragment_Guangchang.getInStanceFragment()) {
				Fragment_Guangchang fragment = new Fragment_Guangchang();
				Bundle arguments = new Bundle();
		       <strong> arguments.putInt("current_id", arg0.getId());</strong>
		        fragment.setArguments(arguments);
				setcurrentFragment(fragment);
			} else {
				setcurrentFragment(Fragment_Guangchang.getInStanceFragment());
			}

			break;
Fragment代码如下

public class Fragment_Luntan extends Base_Fragment {


	public static Fragment_Luntan inStanceFragment = null;


	private Bundle savedState;


	private TextView list_nodata;
	private ListView luntan_listViews;


	private JSONArray tongzhijs = new JSONArray();
	private JSONArray luntan_ja;


	private LuntanListAdapter lla;


	private AutoScrollViewPager picviewpager;
	private LinearLayout contianer;


	@Override
	public void onSaveInstanceState(Bundle outState) {
		saveStateToArguments();


		super.onSaveInstanceState(outState);
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View fragmentView = inflater.inflate(R.layout.fragment_luntan,
				container, false);
		luntan_listViews = (ListView) fragmentView
				.findViewById(R.id.luntan_listViews);
		list_nodata = (TextView) fragmentView.findViewById(R.id.list_nodata);


		contianer = (LinearLayout) fragmentView.findViewById(R.id.contianer);


		inStanceFragment = this;


		return fragmentView;
	}


	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);


		if (!restoreStateFromArguments()) {
			getInfo_LuntanAd();
			get_PatrolData();
		}
	}


	@Override
	public void onDestroyView() {
		super.onDestroyView();
		// Save State Here
		saveStateToArguments();
	}


	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		inStanceFragment = null;


		super.onDestroy();
	}


	private void saveStateToArguments() {
		if (getView() != null)
			savedState = saveState();
		if (savedState != null) {
			Bundle b = getArguments();
			b.putBundle("Fragment_Luntan_InternalSavedViewState",
					savedState);
		}
	}


	private boolean restoreStateFromArguments() {
		Bundle b = getArguments();
		if (null != b
				&& b.getBundle("Fragment_Luntan_InternalSavedViewState") != null) {
			savedState = b
					.getBundle("Fragment_Luntan_InternalSavedViewState");
			restoreState();
			return true;
		}
		return false;
	}


	private void restoreState() {
		if (savedState != null) {
			onRestoreState(savedState);
		}
	}


	// 恢复数据
	protected void onRestoreState(Bundle savedInstanceState) {
		try {
			tongzhijs = new JSONArray(savedInstanceState.getString("tongzhijs"));
			contianer.removeAllViews();
			contianer.addView(initViewPager());


			luntan_ja = new JSONArray(savedInstanceState.getString("luntan_ja"));


			if (luntan_ja.length() > 0) {
				list_nodata.setVisibility(View.GONE);
				luntan_listViews.setVisibility(View.VISIBLE);


				lla = new LuntanListAdapter();
				luntan_listViews.setAdapter(lla);
			} else {
				list_nodata.setVisibility(View.VISIBLE);
				luntan_listViews.setVisibility(View.GONE);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	private Bundle saveState() {
		Bundle state = new Bundle();
		onSaveState(state);


		return state;
	}


	// 保存数据
	protected void onSaveState(Bundle outState) {
		outState.putString("tongzhijs", tongzhijs.toString());
		outState.putString("luntan_ja", luntan_ja.toString());
	}


	
}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值