mercredi 22 juin 2016

Checkbox Startup Data

I am trying to create an Activity that when the application starts up it will check if the checkbox is checked. If it is checked I want it to open up a second Activity where it opens up the NavDrawer instead of the current activity. Any input would be helpful. Thank you in advance.

    package com.example.platinumirish.runassistwithdrawer;

    import android.content.Intent;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import com.example.platinumirish.runassistwithdrawer.IntroPage2Activity;
    import com.example.platinumirish.runassistwithdrawer.NavDrawer;
    import com.example.platinumirish.runassistwithdrawer.R;

    public class MainActivity extends AppCompatActivity {

    private static final String Startup_Identify = "checkbox_setting";

    private CheckBox mCheckBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCheckBox = (CheckBox) findViewById(R.id.checkBox);

        // Set the initial state of the check box based on saved value
        mCheckBox.setChecked(isCheckedSettingEnabled());

        Button btnOne =(Button)findViewById(R.id.NextP1_P2);

        btnOne.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getApplicationContext(),IntroPage2Activity.class);
                startActivity(intent);

            }
        });

        Button btnTwo =(Button)findViewById(R.id.NextP1_Main);

        btnTwo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(getApplicationContext(),NavDrawer.class);
                startActivity(intent);

            }
        });
    }


    @Override
    public void onPause() {
        super.onPause();

        // Persist the setting. Could also do this with an OnCheckedChangeListener.
        setCheckedSettingEnabled(mCheckBox.isChecked());
    }

    /**
     * Returns true if the setting has been saved as enabled,
     * false by default
     */
    private boolean isCheckedSettingEnabled() {
        return PreferenceManager.getDefaultSharedPreferences(this)
                .getBoolean(Startup_Identify, false);
    }

    /**
     * Persists the new state of the setting
     *
     * @param enabled the new state for the setting
     */
    private void setCheckedSettingEnabled(boolean enabled) {
        PreferenceManager.getDefaultSharedPreferences(this)
                .edit()
                .putBoolean(Startup_Identify, enabled)
                .apply();
    }
}




Aucun commentaire:

Enregistrer un commentaire