片段中未调用onSaveInstanceState [英] onSaveInstanceState is not being called in Fragment

查看:120
本文介绍了片段中未调用onSaveInstanceState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道人们已经提出了这个问题,但我遵循了所有的答案,我仍然遇到同样的问题。我有两个脚本一个是片段管理器(IngredientsActivity),另一个是片段(OtherList)。代码如下

I know that people have asked this question, but I followed all the answers and I still have the same problem. I have two scripts One is the fragment manager (IngredientsActivity) and the other is the fragment (OtherList). The code is as follows

IngredientsActivity

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;



public class IngredientsActivity extends FragmentActivity implements ActionBar.TabListener {

    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_list);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // For each of the sections in the app, add a tab to the action bar.
        actionBar.addTab(actionBar.newTab().setText("Alcahol").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Juice").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Other").setTabListener(this));
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {

        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);          //OVERRIDE SAVE ON MAINCLASS

        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }



    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {


        if (tab.getPosition() == 0) {
            AlcoholList simpleListFragment = new AlcoholList();
            getSupportFragmentManager().beginTransaction().replace(R.id.containert, simpleListFragment).commit();
        } 
        else if (tab.getPosition() == 1) {
            JuiceList androidlidt = new JuiceList();
            getSupportFragmentManager().beginTransaction().replace(R.id.containert, androidlidt).commit();
        }



        else {

            OtherList androidversionlist = new OtherList();
            getSupportFragmentManager().beginTransaction().replace(R.id.containert, androidversionlist).commit();

        }
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

       public static class DummySectionFragment extends Fragment {
        public DummySectionFragment() {
        }

        public static final String ARG_SECTION_NUMBER = "section_number";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            TextView textView = new TextView(getActivity());
            textView.setGravity(Gravity.CENTER);
            Bundle args = getArguments();
            textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
            return textView;
        }
    }



}

OtherList

import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;


public class OtherList  extends ListFragment{
    MyCustomAdapter dataAdapter = null;
    private  ArrayList<String> recipesList;
private ArrayList<Items> stateList ;

public OtherList() {

    setRetainInstance(true);
     stateList = new ArrayList<Items>();

          Items _states1 = new Items ("Gin",false);
          stateList.add(_states1);
Items _states2 = new Items ("Ginger Liqueur",false);
          stateList.add(_states2);
Items _states3 = new Items ("Citrus Vodka",false);
          stateList.add(_states3);
Items _states4 = new Items ("Champagne",false);
          stateList.add(_states4);
Items _states5 = new Items ("Coconut Rum",false);
          stateList.add(_states5);
Items _states6 = new Items ("Pear Vodka",false);
          stateList.add(_states6);
Items _states7 = new Items ("Rum",false);
          stateList.add(_states7);
Items _states8 = new Items ("Tequila",false);
          stateList.add(_states8);
Items _states9 = new Items ("Triple Sec",false);
          stateList.add(_states9);
Items _states10 = new Items ("Kahlua",false);
          stateList.add(_states10);

        }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


          dataAdapter = new MyCustomAdapter(this.getActivity(),R.layout.da_item, stateList);
          setListAdapter(dataAdapter);    

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.list_fragment, container, false);
    }

    @Override
    public void onListItemClick(ListView list, View v, int position, long id) {


    }

    private class MyCustomAdapter extends ArrayAdapter<Items>
{

 private ArrayList<Items> stateList;

public MyCustomAdapter(Context context, int textViewResourceId, 

ArrayList<Items> stateList) 
{
      super(context, textViewResourceId, stateList);
      this.stateList = new ArrayList<Items>();
      this.stateList.addAll(stateList);
}

  private class ViewHolder
  {

    CheckBox name;
  }

  @Override
 public View getView(int position, View convertView, ViewGroup parent) 
  {

          ViewHolder holder = null;

          Log.v("ConvertView", String.valueOf(position));

          if (convertView == null)
          {

             LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

             convertView = vi.inflate(R.layout.da_item, null);

            holder = new ViewHolder();

            holder.name = (CheckBox) convertView.findViewById(R.id.ingredientbox);

            convertView.setTag(holder);

                      holder.name.setOnClickListener( new View.OnClickListener() 
                      {
                                 public void onClick(View v)  
                                 {
                                   CheckBox cb = (CheckBox) v;
                                   Items _state = (Items) cb.getTag();
                                   _state.setSelected(cb.isChecked());
                                }
                      });

          }
          else
          {
              holder = (ViewHolder) convertView.getTag();
          }

          Items state = stateList.get(position);


          holder.name.setText(state.getName());
          holder.name.setChecked(state.isSelected());

          holder.name.setTag(state);

          return convertView;
  }

}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);



}

     @Override 
     public void onSaveInstanceState(Bundle outState) {
          Log.d("hey","SAVING");                              //NOT SAVING
         super.onSaveInstanceState(outState);
         save();

         outState.putStringArrayList("index", recipesList); 

         }

                }
            }


    }

    }

我找到了多个建议来覆盖onSaveInstanceState,我做了,我发现更多告诉我使用setRetainInstance。我不知道为什么setRetainInstance状态会有用,如果我想保存列表的值。 我的问题 - 为什么不在OtherList类中调用save,或者如果我想在列表视图中保存值(在这种情况下我使用的是复选框),有什么更好的实现方法

I found multiple suggestions that said to override the onSaveInstanceState , which I did and I found even more telling me to use setRetainInstance. I don't know why setRetainInstance state would be helpful if I want to save a value of a list. My question-Why Is it not calling the save in the OtherList class or whats a better method to implement if I want to save the values in the List View (in this case I am using checkboxes)

推荐答案

onSaveInstanceState()仅在Android系统可能需要重新创建时调用片段的特定实例。有许多实例可以远离碎片导航,甚至可以在不会被调用的地方摧毁碎片。有关详细信息,请参阅文档,请阅读 Activity.onSaveInstanceState()文档,因为此处也适用: http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState (android.os.Bundle)
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

onSaveInstanceState() is only called when the Android system may need to recreate that particular instance of the Fragment. There are many instances of navigating away from the Fragment or even destroying the Fragment where it will not be called. Please consult the documentation for more information, please read the Activity.onSaveInstanceState() documentation as well since that applys here also: http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState(android.os.Bundle) http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

编辑:在您的情况下,每次用户导航回片段时都不得重新创建片段。我建议考虑使用ViewPager和FragmentPagerAdapter来自动管理片段。另请看这篇文章: ViewPager和片段 - 什么是存储片段状态的正确方法?

In your case, you must not recreate the fragment every time the user navigates back to the fragment. I recommend considering ViewPager and FragmentPagerAdapter to automatically manage the fragments. Also look at this post: ViewPager and fragments — what's the right way to store fragment's state?

这篇关于片段中未调用onSaveInstanceState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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