简体   繁体   中英

Quit showing an Activity when CheckBox is checked

I have got a DialogActivity with a "do not show anymore" CheckBox.

What I need it to do is exactly like the CheckBox says. When the CheckBox is checked the Activity doesn't have to be displayed to the user anymore, no matter if the app is restarted or killed.

public class PopUpInfoActivity extends Activity {
static final String PREFS = "preference_file";

@Override
public void onCreate(Bundle state) {
    setContentView(R.layout.popupinfo_layout);

    CheckBox chk = (CheckBox) findViewById(R.id.dontshow_checkbox);
    chk.setChecked(PreferenceManager.getDefaultSharedPreferences(this).getBoolean("value", false));

    chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //store isChecked to Preferences
            SharedPreferences settings = getSharedPreferences(PREFS, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("isChecked", false);

            PreferenceManager.getDefaultSharedPreferences(PopUpInfoActivity.this).edit().putBoolean("value", isChecked).apply();
        }
    });

    Intent intent = new Intent(PopUpInfoActivity.this, ChordsListActivity.class);

    if (!chk.isChecked()) {
        // run activity
        super.onCreate(state);
    } else {
        startActivity(intent);
    }
}
}

If I do it like this the App crashes.

If I replace the else with:

else {
    onStop()
}

The code doesn't work as expected.

I would really appreciate if you could help me fix this problem!

EDIT: This is what I have got in my ChordsListActivity , which is the activity that calls the PopUpInfoActivity but I do not get what I should put in my if() statement.

Intent legendaIntent = new Intent(ChordsListActivity.this, PopUpInfoActivity.class);
    if(/*what's here?*/)
        startActivity(legendaIntent);

In that MainActivity read your preference key (Show_Dialog), if it is true then launch PopUpInfoActivity otherwise launch ChordsListActivity.

The logic should be in the Mother Activity

In your PopUpInfoActivity you should only edit the preference key to false if the user checks out the ChekBox

To do so, please create a preferences.xml file under res/xml, inside this file you have to create a key (Boolean) that you will store inside the status of PopUpInfoActivity.Checkbox, the default will be (True).

here is an example

<CheckBoxPreference 
    android:key="Show_Dialog"  
    android:defaultValue="true" />

From your MainActivity you should read this value like this:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    Boolean showDlg = sharedPref.getBoolean("Show_Dialog", true);

if the showDlg is true then you popup your dialog, otherwise continue what you want to do.

of course you need to change the value of Show_Dialog if the user checks the checkbox, you can do it like this:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor preferencesEditor = sharedPref.edit();
    preferencesEditor.putBoolean("Show_Dialog", false);
    preferencesEditor.commit();     

in this way, you can be sure that your main activity will read the Show_Dialog as false next time the user launches your application

Good luck

Firstly, super.onCreate() must be the first call in any Activity's onCreate() .

Secondly, store the value of the SharedPrefence in a boolean instead of directly setting the CheckBox , and use that before setContentView() to finish your current Activity and launch the next one if true. If not, carry on with the UI stuff and anything else you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM