sam
sam

Reputation: 13

android activity is not destroyed on calling of finish()

I am new to android development need some help with killing an activity

My finish() is not killing this activity whenever I start the new activity "survey_question" from my main activty for new survey my counter for yes and no doesnot go to zero

public class survey_question extends Activity {
int flag=0;
private static int yes=0,no=0;
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton rb1,rb2;
String [] question;

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

    tv= (TextView)findViewById(R.id.question_textview);

    rg= (RadioGroup)findViewById(R.id.yesno_RadioGroup);
    rb1= (RadioButton)findViewById(R.id.yes_RadioButton);
    rb2= (RadioButton)findViewById(R.id.no_RadioButton2);
    question=getResources().getStringArray(R.array.string_array_question);
    tv.setText(question[flag]);





}
public void nextbutton (View v){
    RadioButton uans= (RadioButton)findViewById(rg.getCheckedRadioButtonId());
    String ansText= uans.getText().toString();
    if (ansText.equalsIgnoreCase("YES")){
        yes++;

    }
    else{
        no++;
    }
    flag++;
    if (flag<question.length){
        tv.setText(question[flag]);

    }
    else{

        Intent resultIntent =new Intent(survey_question.this,main_survey.class);

        resultIntent.putExtra("yesans", yes);

        resultIntent.putExtra("noans", no);

        setResult(RESULT_OK, resultIntent);
        this.finish();


    }

Upvotes: 0

Views: 978

Answers (2)

Adam Miśtal
Adam Miśtal

Reputation: 735

You declared this variables like static fields:

private static int yes=0,no=0;

Change them to non static.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006574

That is because you are not setting the values to zero.

You have declared yes and no to be static. They will retain their values for the life of the process, not the life of the activity.

Upvotes: 3

Related Questions