使用这种用法先熟悉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());
}
}