1

What my application first does is it loads ListView whose items have invisible CheckBoxes by setting its visibility View.Gone. When the user tabs a menu button then it will turn on and off the CheckBox visibility and some other layouts. Below is the code, I removed some unnecessary parts:

private void editmodeSwitch(boolean flag){
    // get topbar, bottombar, and bottombar2
    LinearLayout topbar = (LinearLayout) findViewById(R.id.task_topbar_linearLayout);
    LinearLayout bottombar = (LinearLayout) findViewById(R.id.task_bottombar1_linearlayout);
    LinearLayout bottombar2 = (LinearLayout) findViewById(R.id.task_bottombar2_linearlayout);

    if(flag){
        isEditmodeOn = true;            

        // make topbar and bottombar2 visilble, but bottombar gone
        topbar.setVisibility(View.VISIBLE);
        bottombar.setVisibility(View.GONE);
        bottombar2.setVisibility(View.VISIBLE);

        // make checkboxes visible in listview visible as well
        for(int i=0; i<listView.getChildCount(); i++){
            LinearLayout ll = (LinearLayout) listView.getChildAt(i);
            CheckBox cb = (CheckBox) ll.findViewById(R.id.task_row_checkBox1);
            cb.setVisibility(View.VISIBLE);
        }
    }
    else{
        isEditmodeOn = false;

        topbar.setVisibility(View.GONE);
        bottombar.setVisibility(View.VISIBLE);
        bottombar2.setVisibility(View.GONE);

        // set each checkbox false and its visibility gone
        for(int i=0; i<listView.getChildCount(); i++){
            LinearLayout ll = (LinearLayout) listView.getChildAt(i);
            CheckBox cb = (CheckBox) ll.findViewById(R.id.task_row_checkBox1);
            cb.setVisibility(View.GONE);
            cb.setChecked(false);
        }
    }
}

It works fine but the problem is the application doesn't work when the screen rotates(changes the screen orientation). Everything worked fine as it displayed some layouts but only CheckBoxes in list items. Below is the code inonCreate()`:

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

    initialize();
    loadDB();
    updateListAdapter(list_title, list_date);

    // in case of screen rotation
    if(savedInstanceState != null){
        isEditmodeOn = savedInstanceState.getBoolean(EDITMODE_CHECK);
        isItemChecked = savedInstanceState.getBoolean(ITEM_CHECK);

        if(isEditmodeOn){
            if(!isItemChecked){
                Log.i(tag, "item NOT checked");
                editmodeSwitch(true);
            } else{
                //this is something different so please don't mind
                deditmodeSwitch(savedInstanceState.getBooleanArray(LIST_CB_CHECK));
            }
        }
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // save values for rotation
    outState.putBoolean(EDITMODE_CHECK, isEditmodeOn);
    outState.putBoolean(ITEM_CHECK, isItemChecked);
    outState.putBooleanArray(LIST_CB_CHECK, list_cb_check);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    Log.i(tag, "you're in onRestoreInstanceState()");

    // in case of screen rotation
    if(savedInstanceState != null){
        isEditmodeOn = savedInstanceState.getBoolean(EDITMODE_CHECK);
        isItemChecked = savedInstanceState.getBoolean(ITEM_CHECK);

        if(isEditmodeOn){
            if(!isItemChecked){
                Log.i(tag, "item NOT checked");
                editmodeSwitch(true);
            } else{
                // this is for something else so please ignore this part
                editmodeSwitch(savedInstanceState.getBooleanArray(LIST_CB_CHECK));
            }
        }
    }

What I guessed is the ListView is being loaded at the end. Therefore, even if the code in onCreate() makes CheckBoxes visible, the CheckBoxes will become invisible again as its initialization in xml will do so. However, I'm stuck here and need your advice to solve this problem. Can anyone help me?

Just in case, below is the checkbox code of layout xml file for getview.

<CheckBox android:id="@+id/task_row_checkBox1" android:gravity="right" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:visibility="gone"
    />
June
  • 265
  • 1
  • 8
  • 21
  • you are not saving checkbox states in onSaveInstanceState before retriveing it. so first save states in onSaveInstanceState by override `onSaveInstanceState` – ρяσѕρєя K Apr 26 '12 at 16:52
  • Sorry Imran. I'll put my code in onSaveInstanceState() in a minute. Thanks for your advice! – June Apr 27 '12 at 17:27

3 Answers3

1

Override onSaveInstanceState for saving value on screen rotation and onRestoreInstanceState as:

protected void onCreate(Bundle savedInstanceState) {

    if(null != savedInstanceState)
            {
                Boolean IntTest = savedInstanceState.getBoolean("ITEM_CHECK");
                Boolean StrTest = savedInstanceState.getBoolean("ITEM_CHECK");
                Log.e(TAG, "onCreate get the   savedInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);        
            }
}
        @Override
        public void onSaveInstanceState(Bundle savedInstanceState) {
            // Save away the CheckBoxes states, so we still have it if the activity
            // needs to be killed while paused.

          savedInstanceState.putBoolean(EDITMODE_CHECK, 0);
          savedInstanceState.putBoolean(ITEM_CHECK, 0);
          super.onSaveInstanceState(savedInstanceState);
          Log.e(TAG, "onSaveInstanceState");
        } 
        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          Boolean IntTest = savedInstanceState.getBoolean(EDITMODE_CHECK);
          Boolean StrTest = savedInstanceState.getBoolean(ITEM_CHECK);
          Log.e(TAG, "onRestoreInstanceState+IntTest="+IntTest+"+StrTest="+StrTest);
        }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thank you for your idea Imran. However, it didn't solve my problem. After some tests, it seems like the layout xml file of getview, which has the checkbox visibility invisible, is still being loaded at the end after onSaveInstanceState and onRestoreInstanceState. Therefore, I guess they reset my checkboxes View.Gone. I know you already gave me a great idea, but if possible, may I ask your help again? – June Apr 27 '12 at 17:18
  • I guess onRestoreInstanceSate() or bundle param of onCreate() actually worked fine but the only problem in my opinion is that the layout xml file for getview seems like being loaded at the end. As this layout file sets checkboxes View.Gone as initialization, it is why all other functions worked but only checkboxes visibility as it will be reset by the layout file. Therefore, I guess I need to invoke my code after getview is loaded but I'm stuck here(Or maybe my idea is WRONG) Do you have any idea for this Imran? Thanks in advace and thank you for your quick reply! – June Apr 27 '12 at 17:41
  • Show us your onRestoreInstanceState and onSaveInstanceState code, then we can go from there. – Gophermofur Apr 27 '12 at 17:58
  • Sorry that I forgot to upload them. I just did it now. Thank you Gophermofur! – June Apr 27 '12 at 18:02
  • I appreciate your continuous help Gophermofur! I put super.onSaveInstanceState(outState) but unfortunately checkboxes are still hidden. I also read your revised answer, however, it is hard to find where to make a code to set checkboxes visible as it seems like the layout xml for checkboxes is still being loaded later(Or maybe my idea is wrong). Now, I'm starting to think it's maybe good to modify them in getview. Do you think this is good? – June Apr 27 '12 at 18:16
  • @June It might be a cut & paste error, but you should be adding the Override directive to your your onSaveInstanceState method. (Same way you did it for onRestoreInstanceState) – Gophermofur Apr 27 '12 at 18:18
  • Thanks for your quick reply and help Gophermofur. I added @override to the main post. It seems like my problem is very annoying for everyone but I'm glad that so many people are helping me! – June Apr 27 '12 at 18:22
  • Put a Log.i or Log.d command in the onSaveInstance method to make sure you are hitting it. Check you logs... are you hitting both onSaveInstance and onRestore instance? – Gophermofur Apr 27 '12 at 18:47
  • Yes, they both are activated well as the logcat shows. I see they all are called then getview called at the end. Based on this logcat history, I guessed getview(or listview) is being loaded at the end and causes a problem – June Apr 27 '12 at 19:09
0

Similar to how you override onCreate, you can override onConfigurationChanged(...) which you can setup to run when the screen changes orientation.

In order for OnConfigurationChanged(...) to be trigger when the screen rotates, you need to to edit your manifest and put that relationship/rule in.

It's easy to do but takes a bit of explaining and it was answered before in this question: Activity restart on rotation Android

Edit: Here is the dev guide on how to handle configuration changes http://developer.android.com/guide/topics/resources/runtime-changes.html

Edit #2: First, let me suggest using Imran's solution. It follows the Developer Guide better and the end results will be the same.

Now, for the onConfigurationChanged solution.

Look at what you are doing with your onCreate:

1) Set the view. (Checkboxes are hidden at this point. Right?)

2) Call your DB and determine if you should display checkboxes (edit mode)

3) Make all the checkboxes visible.

Now, onConfigurationChanged also calls setContentView, at which point all your checkboxes are hidden again. So you need to repeat the process of making your checkboxes visible (#3 above). You probably don't need to repeat step #2 because the value should be retained, but I'm not sure how the logic of your app works, so you may need to re-do step #2.

Does that make sense?

Community
  • 1
  • 1
Gophermofur
  • 2,101
  • 1
  • 14
  • 14
  • why override OnConfigurationChanged required for this question? – ρяσѕρєя K Apr 26 '12 at 16:53
  • Thank you for your idea and good links Gophermofur. I could learn new tech. and used onConfiguraionChanged() method but it still didn't make checkboxes visible(other layouts worked fine). After some tests, I think the layout xml of getview, which has the checkboxes View.Gone as initialization, is still being loaded at the end. Therefore, it is why checkboxes became invisible as xml file has checkboxes View.Gone and reset checkboxes invisible after onConfigurationChanged() and onRestoreInstanceState() make them visible. I know you already help me a lot, however, may I ask you for more help? – June Apr 27 '12 at 17:25
  • Too long to explain it here so see my revised answer. – Gophermofur Apr 27 '12 at 18:00
0

Based on my experience, getview seems to be triggered at the end and it was why 'onRestoreInstanceState()' and 'onConfigurationChanged()' could not make it as getview will reset my checkboxes invisible as initialization in the layout xml file.

Therefore, the only solution I could find out was I must control them in getview for the answer.

avalancha
  • 1,457
  • 1
  • 22
  • 41
June
  • 265
  • 1
  • 8
  • 21