Question

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);

        }
    });
}
Was it helpful?

Solution

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);


            }
        });

        }

    }



}

OTHER TIPS

CheckBox cb;
SharedPreferences sp;
Button btn;
// never used
//int result;
private final String DONT_SHOW_ACTIVITY = "dontShowActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sp = (SharedPreferences) PreferenceManager
            .getDefaultSharedPreferences(this); 
    // default value is false, means you will start MainActivity at first time.
    // And if you hava checked "dont show again", MainActivity will finish itself 
    // immediately, and start the SecondActivity.
    if(sp.getBoolean(DONT_SHOW_ACTIVITY, false)){
        Intent i = new Intent();
        i.setClass(MainActivity.this, SecondActivity.class);
        startActivity(i);
        this.finish();    
    }        
    setContentView(R.layout.activity_main);
    cb = (CheckBox) findViewById(R.id.checkBox1);
    btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

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

            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean(DONT_SHOW_ACTIVITY,cb.isChecked());
            editor.commit();
            Toast.makeText(getApplicationContext(), "dgdgd", Toast.LENGTH_LONG).show();
            Intent i = new Intent();
            i.setClass(MainActivity.this, SecondActivity.class);
            startActivity(i);
            // you should finish MainActivity here, or you can press back return from 
            // SecondActivity to MainActivity.
            this.finish()
    }
});

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top