0

I can set the shared preferences in a AlertDialog with some radio buttons:

public void ShowRadioDialog() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = preferences.edit();
        SharedPreferences choiceSettings = getSharedPreferences("currentChoice", 0);
        final int[] currentChoice = {choiceSettings.getInt("currentChoice", 0)};

        final CharSequence[] items={"Rosso","Verde","Blu","Giallo","Arancione"};
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Seleziona un colore");
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(DialogInterface dialog, int which) {

            if (index == 1) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                            getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.redDark));
                            toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
                            Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
                            Log.i("Colors", "Rosso Ok");                  
                        }
                } else if (index ==2) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.green));                 
                    }

                } else if (index == 3){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.primary_blue));      
                    }
                } else if (index == 4){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.yellowDark));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.yellow));      
                    }
                } else if (index == 5){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.dark_orange));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.main_orange));      
                    }
                }
                SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
                Editor editor = preferences.edit();
                editor.putInt("choice", index);
                editor.commit();
            }
        });

        builder.setSingleChoiceItems(items,-1, new DialogInterface.OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                    if ("Rosso".equals(items[which])) {
                        index = 1;
                    } else if ("Verde".equals(items[which])) {
                        index = 2;
                    } else if ("Blu".equals(items[which])) {
                        index = 3;
                    } else if ("Giallo".equals(items[which])) {
                        index = 4;
                    } else if ("Arancione".equals(items[which])) {
                        index = 5;
                    }
            }
        });
        builder.show();
    }

In onCreate of the MainActivity i fetch the sharedpreferences in this way:

SharedPreferences preferences = getSharedPreferences("myPref", getApplicationContext().MODE_PRIVATE);
        index = preferences.getInt("choice",-1);
        Log.i("Shared", "Y "+index);

The log is correct! index is in the right position as saved. But it not do the condition in the dialog. I want change the color of status bar and toolbar. When you tap in the dialog the radio button works, but when exit from app and then open it again the colors come back to default. But the state of radio button is saved.. It's so strange..

Atlas91
  • 5,754
  • 17
  • 69
  • 141

1 Answers1

1

Do this in onCreate:

 SharedPreferences preferences = getSharedPreferences("myPref",   getApplicationContext().MODE_PRIVATE);
    index = preferences.getInt("choice",-1);

 if (index == 1) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.redDark));
                        toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.red));
                        Toast.makeText(MainActivity.this, "Rosso OK", Toast.LENGTH_SHORT).show();
                        Log.i("Colors", "Rosso Ok");                  
                    }
            } else if (index ==2) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.green_welcome));
                    toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.green));                 
                }

            } else if (index == 3){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.primary_dark_blue));
                    toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.primary_blue));      
                }
            } else if (index == 4){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.yellowDark));
                    toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.yellow));      
                }
            } else if (index == 5){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    getWindow().setStatusBarColor(MainActivity.this.getResources().getColor(R.color.dark_orange));
                    toolbar.setBackgroundColor(MainActivity.this.getResources().getColor(R.color.main_orange));      
                }
XH6 user
  • 3,460
  • 1
  • 12
  • 14
  • Yeeeeah! It works! mmh i thought was necessary only set the preferences.. Thank you! – Atlas91 Dec 24 '14 at 17:20
  • Don't use the complete code in `onCreate()`. Move it to `onResume()` – Rohit5k2 Dec 24 '14 at 17:22
  • 5 minutes and i can accept :) ok, why in onResume()? – Atlas91 Dec 24 '14 at 17:23
  • 1
    OnResume() will only work for resume. If the activity was already running. If you want activity to retain after app finished. it you need it in onCreate() too – XH6 user Dec 24 '14 at 17:24
  • Just saw the code is changing the color of toolbar, so yeah it should be in `onCreate()`. But your idea about `onResume()` is wrong. Please see the activity life cycle. – Rohit5k2 Dec 24 '14 at 19:33