0

I have an Alert Dialog that displays a couple of checkboxes(5 to be more exact). The check boxes filter 5 lists of markers that are displayed on my map, markers that are grouped in hour intervals. I have managed to make everything work except the fact that I do not know how I should write the code in onSaveInstanceState so that the checkboxes values will be kept after a screen rotation. Here is the part of the code that I think it's relevant for the question. Thanks in advance for any help!

public void filterTheMarkers(View view) {

        if (dialog == null) {

            AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(this);
            LayoutInflater inflater = this.getLayoutInflater();
            @SuppressLint("InflateParams") View checkBoxView = inflater.inflate(R.layout.markers_filtering, null);
            builder.setView(checkBoxView);
            cbAllDay = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
            cbBefore12 = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);
            cbBetween1216 = (CheckBox) checkBoxView.findViewById(R.id.checkBox3);
            cbBetween1620 = (CheckBox) checkBoxView.findViewById(R.id.checkBox4);
            ccbAfter20 = (CheckBox) checkBoxView.findViewById(R.id.checkBox5);

            dialog = builder.create();

        }
        dialog.show();
    }


public void displaySelectedMarkers(View view) {


        dialog.dismiss();
        Log.i("TAG", "All Day " + cbAllDay.isChecked() + " Before 12 " + cbBefore12.isChecked() + " Between 12-16 " + cbBetween1216.isChecked() + " Between 16-20" + cbBetween1620.isChecked() + " After 20 " + ccbAfter20.isChecked());
        //according these check boxes status execute your code to show/hide markers

        if (cbAllDay.isChecked() && cbBefore12.isChecked() && cbBetween1216.isChecked() && cbBetween1620.isChecked() && ccbAfter20.isChecked()) {
            // show all markers
            for (Marker marker : allDayList) {
                marker.setVisible(true);
            }
            for (Marker marker : before12List) {
                marker.setVisible(true);
            }
            for (Marker marker : between1216List) {
                marker.setVisible(true);
            }
            for (Marker marker : between1620List) {
                marker.setVisible(true);
            }
            for (Marker marker : after20List) {
                marker.setVisible(true);
            }
        } else if (cbAllDay.isChecked() && !cbBefore12.isChecked() && !cbBetween1216.isChecked() && !cbBetween1620.isChecked() && !ccbAfter20.isChecked()) {
            // show only All Day Markers
            for (Marker marker : allDayList) {
                marker.setVisible(true);
            }
            for (Marker marker : before12List) {
                marker.setVisible(false);
            }
            for (Marker marker : between1216List) {
                marker.setVisible(false);
            }
            for (Marker marker : between1620List) {
                marker.setVisible(false);
            }
            for (Marker marker : after20List) {
                marker.setVisible(false);
            } // and it goes like this forever covering every possible interval
                        .
                        .
                        .
     }

    }

    public void doNothing(View view) {

        dialog.dismiss();
    }

UPDATE

. . .

Boolean keepMarkerFiltersCB1;
Boolean keepMarkerFiltersCB2;
Boolean keepMarkerFiltersCB3;
Boolean keepMarkerFiltersCB4;
Boolean keepMarkerFiltersCB5;

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

   SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


        if (savedInstanceState == null){
            mapTypeSelected = GoogleMap.MAP_TYPE_NORMAL;
            mSetCameraPosition = true;
            mapFragment.setRetainInstance(true);
            initialMarkers = true;

        } else {
            mapTypeSelected = savedInstanceState.getInt("the_map_type", GoogleMap.MAP_TYPE_NORMAL);
            mSetCameraPosition = false;
            initialMarkers = false;
            keepMarkerFiltersCB1 = savedInstanceState.getBoolean("checkBox1");
            keepMarkerFiltersCB2 = savedInstanceState.getBoolean("checkBox2");
            keepMarkerFiltersCB3 = savedInstanceState.getBoolean("checkBox3");
            keepMarkerFiltersCB4 = savedInstanceState.getBoolean("checkBox4");
            keepMarkerFiltersCB5 = savedInstanceState.getBoolean("checkBox5");
        }

   public void filterTheMarkers(View view) {

        if (dialog == null) {

            AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(this);
            LayoutInflater inflater = this.getLayoutInflater();
            @SuppressLint("InflateParams") View checkBoxView = inflater.inflate(R.layout.markers_filtering, null);
            builder.setView(checkBoxView);
            cbAllDay = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);
            cbBefore12 = (CheckBox) checkBoxView.findViewById(R.id.checkBox2);
            cbBetween1216 = (CheckBox) checkBoxView.findViewById(R.id.checkBox3);
            cbBetween1620 = (CheckBox) checkBoxView.findViewById(R.id.checkBox4);
            ccbAfter20 = (CheckBox) checkBoxView.findViewById(R.id.checkBox5);

            keepMarkerFiltersCB1 = cbAllDay.isChecked();
            keepMarkerFiltersCB2 = cbBefore12.isChecked();
            keepMarkerFiltersCB3 = cbBetween1216.isChecked();
            keepMarkerFiltersCB4 = cbBetween1620.isChecked();
            keepMarkerFiltersCB5 = ccbAfter20.isChecked();


            dialog = builder.create();

        }
        dialog.show();
    }

   public void displaySelectedMarkers(View view) {


        dialog.dismiss();
        Log.i("TAG", "All Day " + cbAllDay.isChecked() + " Before 12 " + cbBefore12.isChecked() + " Between 12-16 " + cbBetween1216.isChecked() + " Between 16-20" + cbBetween1620.isChecked() + " After 20 " + ccbAfter20.isChecked());
        //according these check boxes status execute your code to show/hide markers



        if (cbAllDay.isChecked() && cbBefore12.isChecked() && cbBetween1216.isChecked() && cbBetween1620.isChecked() && ccbAfter20.isChecked()) {
            // show all markers
            for (Marker marker : allDayList) {
                marker.setVisible(true);
            }
            for (Marker marker : before12List) {
                marker.setVisible(true);
            }
            for (Marker marker : between1216List) {
                marker.setVisible(true);
            }
            for (Marker marker : between1620List) {
                marker.setVisible(true);
            }
            for (Marker marker : after20List) {
                marker.setVisible(true);
            }
        } else if (cbAllDay.isChecked() && !cbBefore12.isChecked() && !cbBetween1216.isChecked() && !cbBetween1620.isChecked() && !ccbAfter20.isChecked()) {
            // show only All Day Markers
            for (Marker marker : allDayList) {
                marker.setVisible(true);
            }
            for (Marker marker : before12List) {
                marker.setVisible(false);
            }
            for (Marker marker : between1216List) {
                marker.setVisible(false);
            }
            for (Marker marker : between1620List) {
                marker.setVisible(false);
            }
            for (Marker marker : after20List) {
                marker.setVisible(false);
            }
        } else if
.
.
.


   @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("the_map_type", mapTypeSelected);
        outState.putBoolean("checkBox1", keepMarkerFiltersCB1);
        outState.putBoolean("checkBox2", keepMarkerFiltersCB2);
        outState.putBoolean("checkBox3", keepMarkerFiltersCB3);
        outState.putBoolean("checkBox4", keepMarkerFiltersCB4);
        outState.putBoolean("checkbox5", keepMarkerFiltersCB5);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        savedInstanceState.get("the_map_type");
        savedInstanceState.get("checkBox1");
        savedInstanceState.get("checkBox2");
        savedInstanceState.get("checkBox3");
        savedInstanceState.get("checkBox4");
        savedInstanceState.get("checkBox5");

    }

    private void initialLocation(double lat, double lng, float zoom){
        LatLng latLng = new LatLng(lat, lng );
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
        mMap.moveCamera(update);
Alex Simion
  • 93
  • 1
  • 14
  • please accept the answer if it helped you. – ben_joseph Apr 22 '16 at 10:57
  • I have tried this idea before and unfortunately it is not working. I mean I am adding this code but the same thing happens on rotate, the checkboxes are reseted and not only that they do not retain the previous check, they are not working (not filtering anything) after screen rotation... – Alex Simion Apr 22 '16 at 14:07
  • are you setting the values of checkboxes by reading from the bundle in your onCreate method? – ben_joseph Apr 22 '16 at 14:15
  • yes I am, or at least I think I am...I will update the code in the question so you could see better – Alex Simion Apr 22 '16 at 14:19

2 Answers2

1

You can try the following.

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putBoolean("checkbox1",true);
}
ben_joseph
  • 1,660
  • 1
  • 19
  • 24
  • the updated code it's more or less what you were telling, is it not? But it is not working – Alex Simion Apr 22 '16 at 14:28
  • did you debug and see if your `onSaveInstanceState` is called? also see what are the contents of `savedInstanceState` in your `onCreate` – ben_joseph Apr 22 '16 at 14:42
  • Yest it is called as it works perfectly for the outState.putInt("the_map_type", mapTypeSelected); – Alex Simion Apr 22 '16 at 15:04
  • how about the bundle passed in `onCreate()`? – ben_joseph Apr 22 '16 at 17:55
  • please be more explicit, I am new with this so I do not understand exactly what you're asking me – Alex Simion Apr 22 '16 at 17:57
  • as I added the mapFragment.setRetainInstance(true); in the onCreate bundle I had to specify everywhere the fact that i'm retaining the fragment otherwise it would load the same thing with the same markers over and over again .For ex in the onMapReady I had to add a conditional for the initial location if (mSetCameraPosition){initialLocation(TOULOUSE_LAT, TOULOUSE_LNG, 12); } and also for the markers if (initialMarkers){addMarkers2Map();}. Until I added this it did not matter that I added the setRetainInstance(true) in the onCreate bundle as it always loaded the same map with the same markers – Alex Simion Apr 22 '16 at 18:06
  • I'm thinking that it must be something like this in this case, as the filter button with the checkboxes works perfect when I initially open the app. Once I rotate it the checkboxes not only dont retain the checks they do not work anymore. I mean in the log the checks are there, but on the screen nothing happens. so I am thinking that I need a condition for the checkboxes to be applied to the retained map fragment. I don't know how to explain it better than this – Alex Simion Apr 22 '16 at 18:10
  • in the `onRestoreInstanceState()` , use the `savedInstanceState.getBoolean("checkBox1");` to get the value, and use to programatically set(check) the checkbox. – ben_joseph Apr 22 '16 at 18:35
  • `@Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); cbAllDay.setChecked(savedInstanceState.get("checkBox1")); cbBefore12.setChecked(savedInstanceState.get("checkBox2")); } ` – ben_joseph Apr 22 '16 at 18:41
  • I used the savedInstanceState.getBoolean("checkBox1"); and nothing changes. i am trying now to set the check programatically but I do not think that this will help my problem. Thanks again for your interest. If you have any new ideas please feel free to share them – Alex Simion Apr 22 '16 at 18:51
  • Your code asks me to cast it to boolean. Once I do that it this happens: Unboxing of '(Boolean) savedInstanceState.get("checkBox1")' may produce 'java.lang.NullPointerException' – Alex Simion Apr 22 '16 at 18:55
  • I'm sorry, it should have been `cbAllDay.setChecked(savedInstanceState.getBoolean("checkBox1"));` – ben_joseph Apr 23 '16 at 06:57
  • This one throws Attempt to invoke virtual method 'void android.widget.CheckBox.setChecked(boolean)' on a null object reference – Alex Simion Apr 23 '16 at 08:33
  • You should do `cbAllDay = (CheckBox) checkBoxView.findViewById(R.id.checkBox1);` in your onCreate() method – ben_joseph Apr 23 '16 at 11:35
0

"The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id", check documentation.

So try to set ids to all your checkboxes.

Maxim G
  • 1,479
  • 1
  • 15
  • 23