1

I have created an Activity that is displayed as a Dialog using the

   android:theme="@android:style/Theme.Material.Light.Dialog.NoActionBar.MinWidth"

Now I would like to add a "don't show anymore" ThickBox like this:

enter image description here

I've added it using

<android.support.v7.widget.AppCompatCheckBox

but how do I implement it? I need it to prevent it to show the dialog even if the app is restarted.

I would really appreciate if you could help me out!

Daniele
  • 4,163
  • 7
  • 44
  • 95

1 Answers1

2

I'd use the shared preferences to save whether the dialog should be shown or not. I think the example from the Android documentation can help:

public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
        super.onCreate(state);

        // Restore preferences
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean silent = settings.getBoolean("silentMode", false);
        setSilent(silent);
    }

    @Override
    protected void onStop(){
        super.onStop();

        // We need an Editor object to make preference changes.
        // All objects are from android.context.Context
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("silentMode", mSilentMode);

        // Commit the edits!
        editor.commit();
    }
}

(https://developer.android.com/guide/topics/data/data-storage.html#pref)

You can then use the boolean to determine if the dialog should be shown or not (in the example, that would be the boolean silent).

I hope this answers the question!

Desirius
  • 220
  • 1
  • 8
  • thank you for the great answer! do I need to create a Preference File somewhere else? if so, where? – Daniele Jul 08 '16 at 17:57
  • @Daniele You're welcome :) You don't have to create any files. I believe that the only restriction is that the `PREFS_NAME` (from the example) should be unique for all installed apps. But I'm not sure of that. It wouldn' t harm to set it to something unique though. – Desirius Jul 08 '16 at 18:03
  • Thank you I will try this out! – Daniele Jul 09 '16 at 07:39