1

I'm having some trouble with my EditText's on orientation change. For some reason they dont restore whatever was typed in them. I have 2 classes. The main activity and the fragment which goes in the activity Main activity:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(
    setContentView(R.layout.
    getFragmentManager().beginTransaction().replace(R.id.controlsBar, new AddItemFragment()).commit();
    }
}

And the fragment:

public class AddItemFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add_item, container, false);
    EditText item = (EditText) view.findViewById(R.id.itemNameAdd);
    EditText amount = (EditText) view.findViewById(R.id.itemAmount);

    if (savedInstanceState != null) {
        System.out.println(savedInstanceState.getString("item"));
        item.setText(savedInstanceState.getString("item", ""));
        amount.setText(savedInstanceState.getString("amount", ""));
    }

    //item.setText("SOME TEXT");
    //amount.setText("SOME TEXT");
    return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
    EditText item = (EditText) getActivity().findViewById(R.id.itemNameAdd);
    EditText amount = (EditText) getActivity().findViewById(R.id.itemAmount);
    outState.putString("item", item.getText().toString());
    outState.putString("amount", amount.getText().toString());
}
}

The funny thing is that the line "System.out.println(savedInstanceState.getString("item"));" prints out the correct word in the console. And furthermore the outcommented lines where i set the text to "SOME TEXT" also works. Its only when i set the text to savedInstanceState.getString("amount", ""), it wont work

Thank you

sirius
  • 200
  • 6
  • 14
  • So what do you get? Do you get "" (an empty string,/ the default)? Have you tried `item.setText(savedInstanceState.getString("item"));`? – DigitalNinja Mar 24 '15 at 17:45
  • Yea i have tried with item.setText(savedInstanceState.getString("item")); And i dont get "", i just get the hint text – sirius Mar 24 '15 at 18:06
  • Did you get `null`? Are your keys mapped? From the [Android development](http://developer.android.com/reference/android/os/BaseBundle.html#getString%28java.lang.String,%20java.lang.String%29) `getString(String key)` "Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key." – DigitalNinja Mar 24 '15 at 18:08
  • If what is null? Im not sure what you mean with my keys being mapped? Im using a keylistener on the EditText, could that be the reason? – sirius Mar 24 '15 at 20:50
  • "If what is null?" What is returned from the call to `savedInstanceState.getString("item");`. It doesn't make sense that it returns correctly in this line `System.out.println(savedInstanceState.getString("item"));` but not in this line `item.setText(savedInstanceState.getString("item"));`... – DigitalNinja Mar 24 '15 at 21:10
  • I'm sorry I'm not familiar with keyListener, but I have used fragments and editText. I want to try and help you, but I'm really not sure what is causing that behavior. – DigitalNinja Mar 24 '15 at 21:19
  • That is exactly why im having a hard time figuering out what the problem is... How can it show correct in a println when its not showing anything in a settext... I can try to upload my project if you have time to look at it?.. Its not super big – sirius Mar 24 '15 at 21:30
  • Try out my answer below first and let me know how it goes. – DigitalNinja Mar 24 '15 at 21:30

1 Answers1

0

Try adding the line marked "<---- THIS LINE" to public void onSaveInstanceState(Bundle outState):

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); // <---- THIS LINE
EditText item = (EditText) getActivity().findViewById(R.id.itemNameAdd);
EditText amount = (EditText) getActivity().findViewById(R.id.itemAmount);
outState.putString("item", item.getText().toString());
outState.putString("amount", amount.getText().toString());
}

Also, look at using onActivityCreated, and check out the resources in my comment below.

DigitalNinja
  • 1,078
  • 9
  • 17
  • I dont think that a fragmenet has an onRestoreInstanceState method.. The super.onSaveInstanceState(outState); in onSavInstanceState, didnt do the trick unfortunately – sirius Mar 25 '15 at 08:54
  • You're correct, I didn't realize that. I'm going to delete that part. Check out this [post and answer](http://stackoverflow.com/questions/5412746/android-fragment-onrestoreinstancestate). It talks about using `onActivityCreated`. Also check out the Android examples: [Ex1](http://developer.android.com/guide/components/fragments.html#Example) and [Ex2](http://developer.android.com/reference/android/app/Fragment.html#Layout). I feel like those resources should help out. I think there has to be more communication between the activity and the fragment. – DigitalNinja Mar 25 '15 at 16:19