Same Activity Reopens after Pressing Submit Button in Android

153
March 07, 2017, at 5:34 PM

When i press the submit button same activity reopens (clearing all checked radio buttons). This happens only when the user is already registered.

Using firebase. When the user is new then new activity opens (MatchFind.class) which is correct.

Main Part of Code

bSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatabaseReference current_user_db = mDatabase.child(user_id);
                int id1 = r1.getCheckedRadioButtonId();
                int id2 = r2.getCheckedRadioButtonId();
                int id3 = r3.getCheckedRadioButtonId();
                int id4 = r4.getCheckedRadioButtonId();
                int id5 = r5.getCheckedRadioButtonId();
                int id6 = r6.getCheckedRadioButtonId();
                int id7 = r7.getCheckedRadioButtonId();
                int id8 = r8.getCheckedRadioButtonId();
                int id9 = r9.getCheckedRadioButtonId();
                int id10 = r10.getCheckedRadioButtonId();
                if (id1 == -1 || id2 == -2 || id3 == -1 || id4 == -1 || id5 == -1 || id6 == -1 || id7 == -1
                        || id8 == -1 || id9 == -1 || id10 == -1) {
                    Toast.makeText(Questions.this, "Please Answer All The Questions", Toast.LENGTH_SHORT).show();
                } else {
                    rb1 = (RadioButton) findViewById(id1);
                    rb2 = (RadioButton) findViewById(id2);
                    rb3 = (RadioButton) findViewById(id3);
                    rb4 = (RadioButton) findViewById(id4);
                    rb5 = (RadioButton) findViewById(id5);
                    rb6 = (RadioButton) findViewById(id6);
                    rb7 = (RadioButton) findViewById(id7);
                    rb8 = (RadioButton) findViewById(id8);
                    rb9 = (RadioButton) findViewById(id9);
                    rb10 = (RadioButton) findViewById(id10);
                    getAnswers();
                    /*for (int i = 1; i < 11; i++) {
                        current_user_db.child("answer"+i).setValue(ans[i]);
                    }*/
                    current_user_db.child("answer"+1).setValue(ans[1]);
                    current_user_db.child("answer"+2).setValue(ans[2]);
                    current_user_db.child("answer"+3).setValue(ans[3]);
                    current_user_db.child("answer"+4).setValue(ans[4]);
                    current_user_db.child("answer"+5).setValue(ans[5]);
                    current_user_db.child("answer"+6).setValue(ans[6]);
                    current_user_db.child("answer"+7).setValue(ans[7]);
                    current_user_db.child("answer"+8).setValue(ans[8]);
                    current_user_db.child("answer"+9).setValue(ans[9]);
                    current_user_db.child("answer"+10).setValue(ans[10]);
                    Intent intent = new Intent(Questions.this, MatchFind.class);
                    startActivity(intent);
                }
            }
        });

Full Code

public class Questions extends AppCompatActivity {
    private DatabaseReference mDatabase;
    private FirebaseAuth mAuth;
    private Button bSubmit;
    private RadioGroup r1, r2, r3, r4, r5, r6, r7, r8, r9, r10;
    private RadioButton rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8, rb9, rb10;
    private String ans[] = new String[11];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);
        bSubmit = (Button) findViewById(R.id.bSubmit);
        r1 = (RadioGroup) findViewById(R.id.rGroupQ1);
        r2 = (RadioGroup) findViewById(R.id.rGroupQ2);
        r3 = (RadioGroup) findViewById(R.id.rGroupQ3);
        r4 = (RadioGroup) findViewById(R.id.rGroupQ4);
        r5 = (RadioGroup) findViewById(R.id.rGroupQ5);
        r6 = (RadioGroup) findViewById(R.id.rGroupQ6);
        r7 = (RadioGroup) findViewById(R.id.rGroupQ7);
        r8 = (RadioGroup) findViewById(R.id.rGroupQ8);
        r9 = (RadioGroup) findViewById(R.id.rGroupQ9);
        r10 = (RadioGroup) findViewById(R.id.rGroupQ10);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
        mAuth = FirebaseAuth.getInstance();
        final String user_id = mAuth.getCurrentUser().getUid();
        bSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatabaseReference current_user_db = mDatabase.child(user_id);
                int id1 = r1.getCheckedRadioButtonId();
                int id2 = r2.getCheckedRadioButtonId();
                int id3 = r3.getCheckedRadioButtonId();
                int id4 = r4.getCheckedRadioButtonId();
                int id5 = r5.getCheckedRadioButtonId();
                int id6 = r6.getCheckedRadioButtonId();
                int id7 = r7.getCheckedRadioButtonId();
                int id8 = r8.getCheckedRadioButtonId();
                int id9 = r9.getCheckedRadioButtonId();
                int id10 = r10.getCheckedRadioButtonId();
                if (id1 == -1 || id2 == -2 || id3 == -1 || id4 == -1 || id5 == -1 || id6 == -1 || id7 == -1
                        || id8 == -1 || id9 == -1 || id10 == -1) {
                    Toast.makeText(Questions.this, "Please Answer All The Questions", Toast.LENGTH_SHORT).show();
                } else {
                    rb1 = (RadioButton) findViewById(id1);
                    rb2 = (RadioButton) findViewById(id2);
                    rb3 = (RadioButton) findViewById(id3);
                    rb4 = (RadioButton) findViewById(id4);
                    rb5 = (RadioButton) findViewById(id5);
                    rb6 = (RadioButton) findViewById(id6);
                    rb7 = (RadioButton) findViewById(id7);
                    rb8 = (RadioButton) findViewById(id8);
                    rb9 = (RadioButton) findViewById(id9);
                    rb10 = (RadioButton) findViewById(id10);
                    getAnswers();
                    /*for (int i = 1; i < 11; i++) {
                        current_user_db.child("answer"+i).setValue(ans[i]);
                    }*/
                    current_user_db.child("answer"+1).setValue(ans[1]);
                    current_user_db.child("answer"+2).setValue(ans[2]);
                    current_user_db.child("answer"+3).setValue(ans[3]);
                    current_user_db.child("answer"+4).setValue(ans[4]);
                    current_user_db.child("answer"+5).setValue(ans[5]);
                    current_user_db.child("answer"+6).setValue(ans[6]);
                    current_user_db.child("answer"+7).setValue(ans[7]);
                    current_user_db.child("answer"+8).setValue(ans[8]);
                    current_user_db.child("answer"+9).setValue(ans[9]);
                    current_user_db.child("answer"+10).setValue(ans[10]);
                    Intent intent = new Intent(Questions.this, MatchFind.class);
                    startActivity(intent);
                }
            }
        });
    }
    private void getAnswers() {
        if (rb1.getText().toString().equals("Long Term Relationship"))
            ans[1] = "a";
        else
            ans[1] = "b";
        if (rb2.getText().toString().equals("Yes"))
            ans[2] = "a";
        else
            ans[2] = "b";
        if (rb3.getText().toString().equals("Yes"))
            ans[3] = "a";
        else
            ans[3] = "b";
        if (rb4.getText().toString().equals("Bollywood Movies"))
            ans[4] = "a";
        else
            ans[4] = "b";
        if (rb5.getText().toString().equals("Yes"))
            ans[5] = "a";
        else
            ans[5] = "b";
        if (rb6.getText().toString().equals("Yes"))
            ans[6] = "a";
        else
            ans[6] = "b";
        if (rb7.getText().toString().equals("Yes"))
            ans[7] = "a";
        else
            ans[7] = "b";
        if (rb8.getText().toString().equals("Yes"))
            ans[8] = "a";
        else
            ans[8] = "b";
        if (rb9.getText().toString().equals("Yes"))
            ans[9] = "a";
        else
            ans[9] = "b";
        if (rb10.getText().toString().equals("Yes"))
            ans[10] = "a";
        else
            ans[10] = "b";
    }

FindMatch.java

public class MatchFind extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_find);
    }
}
Answer 1

add this flag,FLAG_ACTIVITY_CLEAR_TASK, and call finish() before starting your activity.Perform a clean,then reboot , Preferably invalidate and restart to clear cache.

Answer 2

Are You using firebase authentication for sign in users . You need to implimet Firebase AuthStateListener( ) in On create. And check whether user is signed in Or not there. if user is signed in auth state changed and you can implimet your button on click there.

READ ALSO
How to out put a 2-D array, which is dynamically evaluated in a python code, come out as a table?

How to out put a 2-D array, which is dynamically evaluated in a python code, come out as a table?

At the moment, I use the following code to output an array but it does not come out aligned very well for 2 digit numbers

212
Manual Math.pow using BigDecimal in Java

Manual Math.pow using BigDecimal in Java

I am trying to write a manual code for Mathpow and that too using the BigDecimal data type as i would be working with very small values later

314
how to keep session alive after page refresh jsf

how to keep session alive after page refresh jsf

Please how do I keep the home session alive when a user refreshes the browser ?

430