Karma5 Karma5 - 13 days ago 5
Android Question

Using Shared preferenece , checkbox and button

I am making a program in which I want to use an

Activity

as a splash screen which shows a list of instruction which
i am using as a background image, a checkbox and a button.
I want that when the
CheckBox
is
clicked
or
onChecked
and
then I click the button, that activity should not be seen
again at startup.

enter image description here

This is what I am doing but still no use

CheckBox cb;
SharedPreferences sp;
Button btn;
int result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn = (Button) findViewById(R.id.button1);

cb = (CheckBox) findViewById(R.id.checkBox1);
sp = (SharedPreferences) PreferenceManager
.getDefaultSharedPreferences(this);
OnCheckedChangeListener cb1 = new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(cb.isChecked()){



}
else{


result = sp.getInt("showActivity", -1);
if(result == 0){
Intent i = new Intent();
i.setClass(MainActivity.this, SecondActivity.class);
startActivity(i);
}
}
}
};
cb.setOnCheckedChangeListener(cb1);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("showActivity",cb.isChecked());
editor.commit();
Toast.makeText(getApplicationContext(), "dgdgd", Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.setClass(MainActivity.this, SecondActivity.class);
startActivity(i);

}
});
}

Answer

Basically you need to use Share-Preference to achieve, put condition within onCreate that user checked initially or not and call appropriate screen as per boolean. Try below code.

public class SplashScreen extends Activity {

    SharedPreferences pref;
    boolean state;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pref = getSharedPreferences("PackageName", Context.MODE_PRIVATE);
        state= pref.getBoolean("State", false);

        if(state){
            Intent intent= new Intent(SplashScreen.this,NextActivity.class);
            startActivity(intent);
            finish();
        }else{

            setContentView(R.layout.fragment_main);



        CheckBox chk = (CheckBox)findViewById(R.id.checkBox1);
        Button btn =(Button)findViewById(R.id.button1);


        chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    state=true; 
                }else{
                    state=false;
                }


            }
        });

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Editor edit= pref.edit();
                edit.putBoolean("State", state);
                edit.commit();

                Intent intent= new Intent(SplashScreen.this,NextActivity.class);
                startActivity(intent);


            }
        });

        }

    }



}