简体   繁体   中英

Saving data in Fragment on orientation change

What i am trying to do :: I am trying to save the data entered into edittext and spinner onorientation change

What is happening ::: I am not able to save the data, how can i resolve this

MainActivity.java

public class MainActivity extends FragmentActivity {

    Fragment_A frgObj;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        Log.d("MAIN-ACTIVITY", "onCreate");
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("MAIN-ACTIVITY", "onStart");
        frgObj=Fragment_A.newInstance();

        getSupportFragmentManager().beginTransaction().replace(R.id.container, frgObj).addToBackStack(null).commit();
    }

    public void onSaveInstanceState(Bundle outState){
           getSupportFragmentManager().putFragment(outState,"myfragment",frgObj);
        }
        public void onRetoreInstanceState(Bundle inState){
            frgObj = (Fragment_A) getSupportFragmentManager().getFragment(inState,"myfragment");
        }
}

Fragment_A.java

public class Fragment_A extends Fragment{

    Button btn;
    Spinner spinner;
    EditText editText;
    SeekBar seekBar;

    public static Fragment_A newInstance() {
        Log.d("FRAGMENT-A", "newInstance");

        Fragment_A fragment = new Fragment_A();
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        Log.d("FRAGMENT-A", "onAttach");
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d("FRAGMENT-A", "onCreate");
        super.onCreate(savedInstanceState);


    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.d("FRAGMENT-A", "onSaveInstanceState");

        super.onSaveInstanceState(outState);
        //City Spinner state
        outState.putString("editText", editText.getText().toString());
        outState.putInt("yourSpinner", spinner.getSelectedItemPosition());

    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.d("FRAGMENT-A", "onCreateView");
        View view=inflater.inflate(R.layout.fragment_a, container, false);

        btn=(Button) view.findViewById(R.id.buttonId1);
        spinner=(Spinner) view.findViewById(R.id.spinnerId1);
        editText=(EditText) view.findViewById(R.id.editTextId1);
        seekBar=(SeekBar) view.findViewById(R.id.seekBarId1);

        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.d("FRAGMENT-A", "onActivityCreated");
        super.onActivityCreated(savedInstanceState);

         if (savedInstanceState != null) {
                //City Spinner state
             editText.setText(savedInstanceState.getString("editText"));
             spinner.setSelection(savedInstanceState.getInt("yourSpinner", 0));

            }       
    }

    @Override
    public void onStart() {
        Log.d("FRAGMENT-A", "onStart");
        super.onStart();

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("FRAGMENT-A", "button Clicked");

                Fragment_B frgObj=Fragment_B.newInstance();

                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, frgObj,"Fragment_B").addToBackStack(null).commit();

            }
        });
    }

}

I don't think you need to change anything in your activity other than call super.onSaveInstanceState(Bundle) . Saving the data onSaveInstanceState in the fragment should be enough. I would put the restoring logic in onCreateView instead of onActivityCreated . onCreateView is guaranteed to be called whenever the fragment is recreated however this is not the case with onActivityCreated .

An other thing you can try is not going any of this and using setRetainInstance(true) which should retain your instance variables through orientation change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM