3

In my app, when options are checked in the overflow menu, then the orientation is changed, they become unchecked. Any way to fix this? I know onSavedInstance should be able to help me here, but I don't know how to implement it in my case.

Here's an Example of how my overflow checkboxes are handled in the main activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}



@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {


switch (item.getItemId()) {

case R.id.subtract_option:
    if (item.isChecked()){
        item.setChecked(false);
        subtractMode = false;

    } else{
        item.setChecked(true);
        subtractMode = true;
    }

    return true;

default:
    return super.onMenuItemSelected(featureId, item);

So how would I implement the onSavedInstanceState in this case?

Ahmad
  • 69,608
  • 17
  • 111
  • 137
Alex
  • 143
  • 2
  • 14
  • 1
    http://stackoverflow.com/questions/5556758/handle-orientation-from-portrait-to-landscape-mode-for-checkbox – KOTIOS Jul 23 '13 at 06:14
  • Are You doing any configuration change handling in Your app? Code of that 'overflow' menu might be also useful. – sandrstar Jul 23 '13 at 06:15
  • You should save your checkbox states in onSavedInstanceState()... – shreyas Jul 23 '13 at 06:21
  • No, i didn't gna try what's below. I reposted because my post dried out and i didnt get a specific answer, so i posted this with specific code, and flagged my past one for deletion... – Alex Jul 27 '13 at 06:24
  • @Axtn95 I merged the two questions. However, you should not repeat your question. Please refer to http://stackoverflow.com/help/no-one-answers to learn what to do when you don't get answers. – Gordon Aug 02 '13 at 20:26
  • use onRetainNonConfigurationInstance(). http://stackoverflow.com/questions/3915952/how-to-save-state-during-orientation-change-in-android-if-the-state-is-made-of-m – Pooja Kapuriya Sep 17 '13 at 06:27

3 Answers3

1

It has to do with how Android handles orientation changes. It actually kills your activity and restarts it. There's two ways to fix it.

1)Implement onSaveInstanceState, write all your state to an object (including the values of checkboxes) and implement onRestoreInstanceState to reverse the process.

2)Add android:configChanges="orientation". This will stop the overriding behavior. It will also break the automatic reloading of layouts if you have separate landscape andd portrait layouts.

I recommend route 2 if your don't have separate layouts for the orientations. If you do and your app is simple, I suggest route 1. If you need that and your app is complex you're screwed and in for a world of pain.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Would you mind helping me a bit more if i posted some code? I definitely have to implement onSaveInstanceState, but my brain can't process how to implement it in my case! – Alex Jul 23 '13 at 09:54
1

override onSaveInstanceState method.

 @Override
    public void onSaveInstanceState( Bundle outState )
    {
        super.onSaveInstanceState( outState );
        outState.putInt( "position", this.position );

    }

and on onCreateView

if ( savedInstanceState != null )
{
            int temp;
            temp = savedInstanceState.getInt( "position", -1 );
            Log.i( LOG_TAG, "temp....." + temp );
           // do whatever you want
}

Make appropriate changes as per your need.

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
  • Can you be a bit more specific please? Im a noob, so i needed help to actually implement with my code. What would i put in onsaveinstancestate? – Alex Jul 27 '13 at 07:19
  • you can save the last state of your checkbox. let say it has checked than put 1 and if not than put 0 . now in savedInstanceState.getInt( "position", -1 ); you will get the value according to that you will set the value to your checkbox again – KDeogharkar Jul 27 '13 at 07:21
  • How do i reference the checkbox state? I check and uncheck options using the Item tag in the onMenuItemSelected, how would i do something similar in the onSaveInstanceState? Please bear with me lol – Alex Jul 27 '13 at 20:05
0

Visit How to save state during orientation change in Android if the state is made of my classes?

he says to use onRetainNonConfigurationInstance() method to save your data on orientation changes.

Community
  • 1
  • 1