SunnyM
SunnyM

Reputation: 27

How do I pass the number of clicks and reset the count in another activity?

The codes are messy at this point since I've been going back and forth so much. Every time user clicks the yes/no button I want the results of counts the button has been clicked to display in another activity. I also want to reset the number of clicks from the second activity as well. All that's needed in the first activity is the question and the yes/no button. Is this possible? Thanks in advance.

public class MainActivity extends AppCompatActivity {

private static final String TAG = "SurveyActivity";
private static final String YES_INDEX = "yes votes";
private static final String NO_INDEX = "no votes";

Button mYesButton;
Button mNoButton;
Button mResetButton;
TextView mSurveyQuestion;

private int yesVoteCount = 0;
private int noVoteCount = 0;
private int resetVotes = 0;


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

    // Use res ID to retrieve inflated objects and assign to variables
    mYesButton = findViewById(R.id.yes_button);
    mNoButton = findViewById(R.id.no_button);
    mResetButton = findViewById(R.id.reset_button);
    mSurveyQuestion = findViewById(R.id.survey_question);

    mYesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addVote();
        }
    });

    mNoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addVote();
        }
    });
    // Resetting vote count
    mResetButton.setOnClickListener(new View.OnClickListener() {
        @Override
        ***Should this supposed to be in the second activity?
        }
    });
}
private void addVote() {
    if (mYesButton.isPressed()) {
        yesVoteCount++;
    } else if (mNoButton.isPressed()) {
        noVoteCount++;

}

}

Upvotes: 1

Views: 138

Answers (2)

Agnaramon
Agnaramon

Reputation: 881

In your main activity

btnShowResut.setOnClickListener(new View.OnClickListener() {
   @Override
   // Create intent for going to another activity
   Intent intent = new Intent(this, AnotherActivity.class);
   // Put counts datas to intent
   intent.putExtra("yesCountKey", yesVoteCount);
   intent.putExtra("noCountKey", noVoteCount);


   // NEW : Go to another activity by calling it instead
   // REQUEST_CODE is an integer variable
   startActivityForResult(intent, REQUEST_CODE);

   }
});

In Another activity, you can retrieve datas in onCreate method like this and send action to clear counts of your main activity.

...
onCreate(...){
  ...
  // Retrieve datas from intent
  int yesCount = getIntent().getIntExtra("yesCountKey", 0);
  int noCount = getIntent().getIntExtra("noCountKey", 0);

  mResetButton.setOnClickListener(new View.OnClickListener() {
   @Override

     // Send a boolean to main activity for clearing votes
     Intent intent = new Intent();
     intent.putExtra("resetVotes", true);
     setResult(RESULT_OK, intent);
     // Close second activity
     finish();
   }
});

}

Finally in the main activity override this method and clear votes

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  if(requestCode == 2000 && resultCode == RESULT_OK){ 
    boolean reset = data.getBooleanExtra("resetVotes", false);
    if(reset){
      yesVoteCount = 0;
      noVoteCount = 0;
    }
   }
}

Upvotes: 1

Ben Groseclose
Ben Groseclose

Reputation: 51

As the mentioned above, you can get the counts by using intent extras.

However if you want to reset the counts in in the second activity you might want to start the Activity B as startActivityForResult() see the Android documentation here.

Then when Activity B end you can reset the counts in the call back method onActivityResult().

If you don't want to do it like this the next best way might be to reset the counts onResume() of Activity A so that when you return to the activity you will start with fresh counts. See life cycle documentation here

Upvotes: 0

Related Questions