dimanche 28 août 2016

Checkbox becomes redundant after returning to a view

I am trying to have a checkbox on my homepage, which when checked, should take me to an another view, else, displays an error message on the same view.

This works fine the first time only. If unchecked, it displays the error, and if checked, renders the next view.

However, when I return to this homepage using back button, this time the checkbox becomes redundant. Even unchecking it renders the next page correctly.

If I remove all the views of the ViewGroup, it removes them permanently, and the homepage is empty once I return to it. I believe the checkbox needs to be reset every time I return to the view. However, I am unable to do the same.

Please find my code below(I am a beginner to Android development):

    public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE="com.android.AI";
public boolean isCheckBoxClicked=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
    //isChecked is the onClick attribute for checkbox in the main.xml here
public void isChecked(View view){
    boolean isChecked= ((CheckBox) view).isChecked();
    if(isChecked){
        isCheckBoxClicked=true;
    }
    else{
        isCheckBoxClicked=false;
    }
}
//Send message is onClick for a submit button
public void sendMessage(View view){
    if(isCheckBoxClicked) {
        Intent intent = new Intent(this, SeniorTeam.class);
        startActivity(intent);
    }
    else {
        TextView acceptTerms = new TextView(this);
        acceptTerms.setTextSize(15);
        acceptTerms.setText("Please accpet terms and conditions before proceeding");
        ViewGroup terms = (ViewGroup) findViewById(R.id.activity_main);
        terms.addView(acceptTerms);
    }
}

}




Aucun commentaire:

Enregistrer un commentaire