how to save menuitem visibility state through sharedpreferences?
Asked Answered
G

2

1

Hi In my app I add a menuitem on the action bar called "add to favorite" shown by white star icon. When user click it this icon become disappear and other yellow star icon item visibility comes up...this works fine..but the problem is that when I switch to another activity or when I restart my app the previous event has been lost. How can I save this through sharedpreferences? ?.. Here my code.

On prepareoptionmenu (Menu menu){
If (ffavClicked){
menu.finditem (R.id.id_favorite).setvisible (false);
menu.finditem (R.id.id_favorite 2). setvisible (true);
}
else if (! favClicked){
menu.finditem ( R.id.id_favorite).setvisible (true);
menu.finditem (R.id.id_favorite 2).setvisible (false);
}
return ssuper . onprepareoptionmenu (menu);
}
Gonick answered 23/2, 2014 at 18:55 Comment(0)
B
1

i hope this will solve your problem!

in your activity define boolean first

public class Atherosclerosis extends Activity {

    boolean favClicked;

use this in your on create.

setContentView(R.layout.activity_main);

        SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        favClicked = myPrefs.getBoolean("menu_item", false);

and then

 public boolean onOptionsItemSelected(MenuItem item) {


          SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(this);
          final SharedPreferences.Editor editor = myPrefs.edit();
          favClicked = myPrefs.getBoolean("menu_item", false);


          switch (item.getItemId()) {
           case R.id.id_favorit:
                favClicked=true;
                editor.putBoolean("menu_item", favClicked);
                editor.commit();
                invalidateOptionsMenu();
                return true;

            case R.id.id_favorit2:

                favClicked=false;
                editor.putBoolean("menu_item", favClicked);
                editor.commit();
                invalidateOptionsMenu();
                return super.onOptionsItemSelected(item); 
            }
        return true;
        }
           @Override
            public boolean onPrepareOptionsMenu(Menu menu) {


         if(favClicked==true){
               menu.findItem(R.id.id_favorit).setVisible(false);
                menu.findItem(R.id.id_favorit2).setVisible(true);

         }else{
           menu.findItem(R.id.id_favorit).setVisible(true);
            menu.findItem(R.id.id_favorit2).setVisible(false);

     }



                return super.onPrepareOptionsMenu(menu);
            }


   }
Bruno answered 25/2, 2014 at 18:22 Comment(0)
W
0

I hope this will help you:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    SharedPreferences prefs = getSharedPreferences("YOURprefsName",
            Context.MODE_PRIVATE);

    ffavClicked = prefs.getBoolean("favClicked", false);

    if (ffavClicked){
        menu.finditem (R.id.id_favorite).setvisible (false);
        menu.finditem (R.id.id_favorite 2). setvisible (true);
    } else if (!ffavClicked){
        menu.finditem ( R.id.id_favorite).setvisible (true);
        menu.finditem (R.id.id_favorite 2).setvisible (false);
    }

    return super.onPrepareOptionsMenu(menu);
}

@Override
protected void onPause() {
    SharedPreferences prefs = getSharedPreferences("YOURprefsName",
            Context.MODE_PRIVATE);
    prefs.edit().putBoolean("favClicked", ffavClicked).commit();

    super.onPause();
}
Wiseacre answered 23/2, 2014 at 19:34 Comment(2)
Oppsss something wrong..now with this change, other item not appearing and the first one not disappear. . What should I do now?Gonick
Not working please look what is missing..visibility option is not workingGonick

© 2022 - 2024 — McMap. All rights reserved.