简体   繁体   English

选中CheckBox时退出以显示活动

[英]Quit showing an Activity when CheckBox is checked

I have got a DialogActivity with a "do not show anymore" CheckBox. 我有一个带有“不再显示”复选框的DialogActivity。

What I need it to do is exactly like the CheckBox says. 我需要它做的就像CheckBox所说的一样。 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. 选中CheckBox后,无论应用程序是重新启动还是终止,都不必再向用户显示“活动”。

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替换为:

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. 编辑:这是我在ChordsListActivity获得的ChordsListActivity ,它是调用PopUpInfoActivity的活动,但我没有得到应放在if()语句中的内容。

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. 在该MainActivity中,读取您的首选项键(Show_Dialog),如果为true,则启动PopUpInfoActivity,否则启动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 在PopUpInfoActivity中,仅当用户签出ChekBox时,才应将首选项键编辑为false。

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). 为此,请在res / xml下创建一个preferences.xml文件,在该文件中,您必须创建一个密钥(布尔值),并将其存储在PopUpInfoActivity.Checkbox状态下,默认值为(真)。

here is an example 这是一个例子

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

From your MainActivity you should read this value like this: 您应该从MainActivity中读取以下值:

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. 如果showDlg为true,则弹出对话框,否则继续您要执行的操作。

of course you need to change the value of Show_Dialog if the user checks the checkbox, you can do it like this: 当然,如果用户选中了该复选框,则需要更改Show_Dialog的值,您可以这样做:

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 这样,您可以确保下次用户启动您的应用程序时,您的主要活动将Show_Dialog读取为false

Good luck 祝好运

Firstly, super.onCreate() must be the first call in any Activity's onCreate() . 首先, super.onCreate()必须是任何Activity的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. 其次,将SharedPrefence的值存储在布尔值中,而不是直接设置CheckBox ,并在setContentView()之前使用它来完成当前的Activity并启动下一个(如果为true)。 If not, carry on with the UI stuff and anything else you need. 如果没有,请继续处理UI内容以及您需要的其他任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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