Search code examples
androidmenuitemandroid-sharedpreferences

Radio Buttons in Menu using SharedPreferences


Trying to have three radio buttons show up as menu items where the user picks only one and that choice is saved even if the app is closed. The choice they pick will then be used to determine which layout fragment will be displayed when the app starts up.

Don't really understand how to save the user choice in shared preferences as I have never used it before and new to coding.

My Menu Layout is

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/settings"
    android:title="@string/settings">
    <menu >
        <group android:id="@+id/startScreenGroup" android:checkableBehavior="single">
            <item android:id="@+id/submenu0" android:title="Fragment 0" />
            <item android:id="@+id/submenu1" android:title="Fragment 1" />
            <item android:id="@+id/submenu2" android:title="Fragment 2" />
        </group>
    </menu>
</item>

</menu>

Solution

  • Try the following code, It's not tested but basically this is how it usually would be, please let me know if there's any errors that occur when trying my code.

    private RadioGroup radioGroup;
    private RadioButton radioButton;
    
    //Fragment references
    private Fragment0 fragment0;
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_location_detail);
    
            //Create a SharedPreference variable
            SharedPreferences appPreferences = this.getSharedPreferences("YOUR APP PACKAGE NAME", Context.MODE_PRIVATE);
    
    
            final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.startScreenGroup);
           radioGroup.setOnChangedListener(new RadioGroup.OnChangedListener() {
    
              @Override
             void onCheckedChanged(RadioGroup group, int checkedId) {
    
                switch (checkedId) {
    
                case R.id.submenu0:
                     appPreferences.edit().putString("LoadFragment","submenu0");
                     break;
                case R.id.submenu1:
                     appPreferences.edit().putString("LoadFragment","submenu1");
                     break;
                case R.id.submenu2:
                    appPreferences.edit().putString("LoadFragment","submenu2");
                    break;
                 }
    
            appPreferences.commit();        
        }
    });
    
    
            //get menuID
            String fragmentToLoad = appPreferences.getString("LoadFragment","");
    
           //load user selected fragment
           if(fragmentToLoad != null){
    
           switch(fragmentToLoad){
              case "submenu0" : 
              this.getSupportFragmentManager().beginTransaction()
                  .replace(R.id.container_main, fragment0)
                  .commit();
                  break;
    
             case "submenu1" : 
             this.getSupportFragmentManager().beginTransaction()
                 .replace(R.id.container_main, fragment1)
                 .commit();
                 break;
    
             case "submenu2" : 
             this.getSupportFragmentManager().beginTransaction()
                 .replace(R.id.container_main, fragment2)
                 .commit();
                  break;
            }
       }
    
    
    }
    

    UPDATE

    I finally got some time to work on this, 100% working and tested on 2 mobiles (Lollipop and Marshmallow devices).

    public class MainActivity extends AppCompatActivity {
    
        SharedPreferences appPreferences;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            //Create a SharedPreference variable
            appPreferences = this.getSharedPreferences("com.example.ramithrd.radiobuttonssharedpref", Context.MODE_PRIVATE);
    
            //get menuID
            String fragmentToLoad = appPreferences.getString("LoadFragment","");
    
            //load user selected fragment
            if(fragmentToLoad != null){
    
                switch(fragmentToLoad){
                    case "submenu0" :
                        this.getSupportFragmentManager().beginTransaction()
                                .replace(R.id.container_main, new Fragment0())
                                .commit();
                        break;
    
                    case "submenu1" :
                        this.getSupportFragmentManager().beginTransaction()
                                .replace(R.id.container_main, new Fragment1())
                                .commit();
                        break;
    
                    case "submenu2" :
                        this.getSupportFragmentManager().beginTransaction()
                                .replace(R.id.container_main, new Fragment2())
                                .commit();
                        break;
                }
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            int id = item.getItemId();
    
            if (id == R.id.submenu0) {
                appPreferences.edit().putString("LoadFragment","submenu0").commit();
                this.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container_main, new Fragment0())
                        .commit();
    
            } else  if (id == R.id.submenu1) {
                appPreferences.edit().putString("LoadFragment","submenu1").commit();
                this.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container_main, new Fragment1())
                        .commit();
            } else if (id == R.id.submenu2){
                appPreferences.edit().putString("LoadFragment","submenu2").commit();
                this.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container_main, new Fragment2())
                        .commit();
            }
    
    
            return super.onOptionsItemSelected(item);
        }
    }