0

I have tried a every solution I can find and am not having any luck. I have multiple dynamic textViews that change not only text but formatting such as alpha, strike-through... I can not find a reliable way to save this information through screen rotation. I am self taught and new my head is spinning...

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new UIrFragment())
                .commit();
    }
}

}

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    setRetainInstance(true);
    Log.i(Tag, "onCreateView");
    return rootView;
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

        ImageView background = (ImageView) getView().findViewById(R.id.background);
        background.setImageResource(R.drawable.starrynightblurry);
        setupKeyBoard(); // multiple textViews get set up here. Want to save this 
        setupGame();// same here

    }

3 Answers3

0

Add below line in activity_main.xml

<activity android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation" >
Yogesh Lakhotia
  • 888
  • 1
  • 13
  • 26
0

either fix screen orientation to either landscape or portrait in your AndroidManifest.xml like this:

 android:screenOrientation="portrait"

or handle the configChanges like this:

 android:configChanges="orientation|keyboardHidden|smallestScreenSize|uiMode"

should be added as an attribute of the <activity> tag in the manifest. The configChanges is used to specify configuration changes that the activity will handle itself.

Edit: for handling config changes in Fragments, please see this question.

Community
  • 1
  • 1
donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • I want the rotation so the first option is no good. I tried the second but the textViews are still reset. – Jethro _Orange Feb 18 '14 at 07:36
  • really? Are you sure you have added it to the correct activity? Maybe also clean project. – donfuxx Feb 18 '14 at 07:41
  • I copied that code and placed directly under the android:name tag in the – Jethro _Orange Feb 18 '14 at 07:43
  • I see. I think it is because the views are in a Fragment. Let me remember how configChanges were handled with Fragments... – donfuxx Feb 18 '14 at 07:46
  • Try the first answer of this question: http://stackoverflow.com/questions/13305861/fool-proof-way-to-handle-fragment-on-orientation-change – donfuxx Feb 18 '14 at 07:49
  • @user3322233 did handling config changes in fragments solve your problem in the end? – donfuxx Feb 18 '14 at 08:17
  • 1
    Sort of I am now getting a saved state. I think I need to research how to save the multiple (30+) Textviews in the out state. Not just the text but the formatting. Thanks for the help – Jethro _Orange Feb 18 '14 at 18:12
  • I ended up using onSavedInstantState. I just had to properly learn how to handle fragments. Thank you for the direction. – Jethro _Orange Mar 10 '14 at 19:48
0

Apparently when using setRetainInstance(true), the Bundle for onRestoreInstanceState is null.

store text views in

@Override
public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    //store here
}

Restore text view from here

@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);        
    // restore here
}

hope this helps you

Yogesh Lakhotia
  • 888
  • 1
  • 13
  • 26