0

I have already posted this question once and even tried all possible solutions. So please, if are going to downvote, at least provide the correct answer because I have tried out many solutions and nothing is working out.

In my app, I have an EditText inside the fragment which on orientation change, looses its value.One more thing, I am using different layouts for different orientations. Is there any way to retain the fragment data? I am again mentioning "Fragment data".Please find my Fragment code below for further reference.

 @Override
public void onSaveInstanceState(Bundle outState) {

    outState.putString("fname",etxt_firstname.getText().toString());
    outState.putString("lname",etxt_lastname.getText().toString());
    super.onSaveInstanceState(outState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // retain this fragment
    setRetainInstance(true);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(savedInstanceState!= null)
    {
        etxt_firstname.setText(savedInstanceState.getString("fname"));
        etxt_lastname.setText(savedInstanceState.getString("lname"));
    }

}

Inside OnCreateView():

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    shrp_sharedpreferences = myContext.getSharedPreferences("shrp", Context.MODE_PRIVATE);
    View view = null;

    if(!isTablet(myContext))
    {
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            view =  inflater.inflate(R.layout.fragment_home_landscape, container, false);
        }
        else
        {
             view =  inflater.inflate(R.layout.fragment_home, container, false);

        }
    }
    else
    {
        view =  inflater.inflate(R.layout.fragment_home, container, false);
    }
Animesh Jena
  • 1,541
  • 1
  • 25
  • 44

1 Answers1

0

Even though the fragment itself is retained, its view is destroyed when device is rotated in onDestroyView() (You should also clear any references to views there to prevent leaking old orientation).

Trying to restore text in onActivityCreated() does nothing because you're referencing views from old orientation, and they aren't used anymore.

When fragment is reattached (in Activity created for new orientation), new View is created in onCreateView() - this is where you can use savedInstanceState or any fragment fields to restore views to previous state:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // (...) your layout inflation and view binding logic...

    if(savedInstanceState != null){
        etxt_firstname.setText(savedInstanceState.getString("fname"));
        etxt_lastname.setText(savedInstanceState.getString("lname"));
    }
    // return view you inflated
}
Pawel
  • 15,548
  • 3
  • 36
  • 36