0

I'm here to see if they guide me in this problem

I have a menu, with an item, and what I want is that item to work as a button, if you click on it change the icon and a toast that says Notification active, and if you click again, change the icon and say using toast, Disabled notifications.

All this I have achieved, the detail is that when activating the button and leaving the activity and when entering again, the button is again deactivated, when it should be activated

I hope you understand, here I give you the code, which makes the function perfect, but I think I must do something with shared preferences to save if it is clicked or not, but I do not know where to start, I hope you understand and guide me, I leave the code that I have implemented

----- xml menu ------------

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


    <item
        android:icon="@drawable/ic_notifications_off_black_24dp"
        android:title="@string/notificaciones"
        android:id="@+id/notificaciones"
        android:checkable="true"
        app:showAsAction="always"
    />


</menu>

--------- Java---------------

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menucampeonatos, menu);

        MenuItem item = menu.findItem(R.id.notificaciones);
        item.setChecked(false);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem checkable = menu.findItem(R.id.notificaciones);
        checkable.setChecked(false);
        return super.onPrepareOptionsMenu(menu);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){

            case R.id.notificaciones:

                if (item.isChecked()){
                    myRef.update("notificacion", false);

                    item.setChecked(false);
                    item.setIcon(R.drawable.ic_notifications_off_black_24dp);
                    Toast.makeText(this, "Notificaciones Desactivadas para este campeonato", Toast.LENGTH_LONG).show();

                }else{
                    myRef.update("notificacion", true);

                    item.setChecked(true);
                    item.setIcon(R.drawable.ic_notifications_active_black_24dp);
                    Toast.makeText(this, "Notificaciones Activadas para este campeonato", Toast.LENGTH_LONG).show();

                }

                return true;

        }

        return super.onOptionsItemSelected(item);
    }
aAaDesigner
  • 21
  • 1
  • 5

1 Answers1

0

First you need to implement Shared Preferences, as you mentioned

   private String sharedPrefFile = "your Shared Preferences Name";
   private String NOTIFICATION_KEY = "notification_key";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       mPreferences = getSharedPreferences(sharedPrefFile, MODE_PRIVATE);
       ...
   }

Then, in your onOptionsItemSelected, you need to save user action to preference

   public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){

            case R.id.notificaciones:

                if (item.isChecked()){
                    // Add this line
                    SharedPreferences.Editor preferencesEditor = mPreferences.edit();
                    preferencesEditor.putBoolean(NOTIFICATION_KEY, true);
                    preferencesEditor.apply();

                    myRef.update("notificacion", false);

                    item.setChecked(false);
                    item.setIcon(R.drawable.ic_notifications_off_black_24dp);
                    Toast.makeText(this, "Notificaciones Desactivadas para este campeonato", Toast.LENGTH_LONG).show();

                }else{
                    // Add this line
                    SharedPreferences.Editor preferencesEditor = mPreferences.edit();
                    preferencesEditor.putBoolean(NOTIFICATION_KEY, false);
                    preferencesEditor.apply();

                    myRef.update("notificacion", true);

                    item.setChecked(true);
                    item.setIcon(R.drawable.ic_notifications_active_black_24dp);
                    Toast.makeText(this, "Notificaciones Activadas para este campeonato", Toast.LENGTH_LONG).show();

                }

                return true;

        }

        return super.onOptionsItemSelected(item);
    }

Lastly, load the preference everytime activity is destroyed in onPrepareOptionsMenu

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem checkable = menu.findItem(R.id.notificaciones);
        String isNotificationClicked = mPreferences.getBoolean(NOTIFICATION_KEY, false);
        checkable.setChecked(isNotificationClicked);
        return super.onPrepareOptionsMenu(menu);
    }
Franz Andel
  • 1,326
  • 12
  • 20