Android的碎片状态总是空的onCreateView方法和onActivityCreated方法 [英] Android fragment state always null in onCreateView method and onActivityCreated method

查看:664
本文介绍了Android的碎片状态总是空的onCreateView方法和onActivityCreated方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的片段类多个选项卡。我是在拯救片段的状态失败。我用的方法的onSaveInstanceState()并保存参数在里面。但是,当我回到这个片段保存的状态总是显示空。我尝试了所有的问题,并就以this.Still我面临同样的问题的答案。请任何人都可以帮我解决这个问题。

我的code

  @覆盖
 公共无效的onCreate(捆绑savedInstanceState){
   // TODO自动生成方法存根
   super.onCreate(savedInstanceState);
   setRetainInstance(真);
   super.onCreate(savedInstanceState);
   Log.d(savedInstanceStateInfo1,+ savedInstanceState);
   如果(NULL!= savedInstanceState){
    nAndroids = savedInstanceState.getInt(nAndroids);
    Log.d(NANDROIDS,+ nAndroids);
   }
  }
  // Log.d(国家在OnCreate中:,+ savedInstanceState); @覆盖
 公共无效onActivityCreated(捆绑savedInstanceState){
  super.onActivityCreated(savedInstanceState);
  setRetainInstance(真);
  Log.d(SavedinstanestateInfo,+ savedInstanceState);
 } @覆盖
 公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
  捆绑savedInstanceState){
  // TODO自动生成方法存根
  如果(集装箱== NULL){
   返回null;
  }
  //最后的动作条AB = getActivity()getSupportActionBar()。  视图V =(的LinearLayout)inflater.inflate(R.layout.infotab,集装箱,FALSE);
  Log.d(SavedinstanestateInfo,+ savedInstanceState);
  返回伏;
 }
 @覆盖
 公共无效的onSaveInstanceState(捆绑outState){
  outState.putInt(nAndroids,nAndroids);  super.onSaveInstanceState(outState);
 }


解决方案

1)显然使用setRetainInstance(真)时,该套件,onRestoreInstanceState为null。

http://stackoverflow.com/a/8488107/1011746

2)没有必要的,但更习惯进行任何修改outState之前调用super.onSaveInstanceState(outState)。同为super.onRestoreInstanceState(outState)在onRestoreInstanceState(捆绑outState)。

I have used multiple tabs in fragment class. I was failed in saving the state of fragment. I used the method onSaveInstanceState() and save a parameter in it. But when I came back to this fragment the saved state always shows "null". I tried all the questions and answers regarding to this.Still I am facing the same problem. Please could anyone help me to solve this.

MY CODE

 @Override
 public void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   setRetainInstance(true);
   super.onCreate(savedInstanceState);
   Log.d("savedInstanceStateInfo1", "" + savedInstanceState);
   if (null != savedInstanceState) {
    nAndroids = savedInstanceState.getInt("nAndroids");
    Log.d("NANDROIDS", "" + nAndroids);
   }
  }
  // Log.d("State in oncreate : ",""+savedInstanceState);

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  setRetainInstance(true);
  Log.d("SavedinstanestateInfo", "" + savedInstanceState);
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  if (container == null) {
   return null;
  }
  // final ActionBar ab=getActivity().getSupportActionBar();

  View v = (LinearLayout) inflater.inflate(R.layout.infotab, container, false);
  Log.d("SavedinstanestateInfo", "" + savedInstanceState);
  return v;
 }
 @Override
 public void onSaveInstanceState(Bundle outState) {
  outState.putInt("nAndroids", nAndroids);

  super.onSaveInstanceState(outState);
 }

解决方案

1) Apparently when using setRetainInstance(true), the Bundle for onRestoreInstanceState is null.

http://stackoverflow.com/a/8488107/1011746

2) Not necessary, but more customary to call super.onSaveInstanceState(outState) before making any modifications to outState. Same for super.onRestoreInstanceState(outState) in onRestoreInstanceState(Bundle outState).

这篇关于Android的碎片状态总是空的onCreateView方法和onActivityCreated方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆