0

Well, I wanted to save value of a variable contained in a dialogFragment when the screen is rotated in Android. I've tried every method I could find on the internet, and none of them has worked for me. Some kill my application, and others simply were not doing anything.

I need a real and effective way to save the value of an EditText that is reset when the device screen rotates. The EditText is in a DialogFragment turn this into a FragmentActivity.

thank you very much

MrCoquish
  • 13
  • 2

3 Answers3

0

I don't know if I understood your question, but you can capture the rotation as follows:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            //save value
        }
         if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            //save value
        } 
    }

If you want that your activity don't restart, you should add this into the manifest file:

<activity
            .....
            android:configChanges="keyboardHidden|orientation|screenSize"
            ..... >
 </activity>
  • No, referring to something like `` `@Override` `public void onSaveInstanceState(Bundle outState){` ` super.onSaveInstanceState(outState);` ` outState.putString("precio", ` `txtPrecio.getText().toString());` `}` but this way has not worked for me, and the like either. – MrCoquish Sep 04 '14 at 18:06
0

Set your fragment's retaingInstance flag to true:

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

This prevents Fragment instance from being recreated.

Also be sure you don't recreate the fragment all the time:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    if (savedInstanceState == null) {
        Fragment f = new Myfragment();
        f.setRetainInstance(true);
        getSupportFragmentManager().beginTransaction()
            .add(R.id.container, f))
            .commit();
    }

}

Since View state is preserved during Activity recreation and your are keeping the same Fragment instance you don't need to save TextView value all the time.

ivan.aguirre
  • 500
  • 4
  • 11
0

I just found out that you can not apply the methods to save bundles automatically to the dialogues, but these would apply to activities or fragments of these dialogues dependent. If I can fix it by code, will put the solution here. thank you

MrCoquish
  • 13
  • 2