How do I receive data (int.) from another Activity?

Asked

Viewed 23 times

0

I researched several posts here on stackoverflow and I found none talking about migrating whole numbers, I tried a lot of things but it didn’t work out. I’m a beginner and I probably missed something silly, but I can’t see what it is.

I am putting together a quiz, with several activitys and wanted to make a score that was adding up 1 point for each right question of each Activity.

Follow my code that is not working. If anyone can help me, I am grateful at once.

Activity 1

public class A_Activity extends AppCompatActivity {

TextView score;
int mScore = 0;

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

    score = (TextView) findViewById(R.id.score);
    score.setText("Score: " + mScore);

}

public void onClickTrue(View v) {
    mScore++;
    score.setText("Score: " + mScore);

    Intent i = new Intent(this, B_Activity.class);

    Bundle params = new Bundle();
    params.putInt("mScore", mScore);
    i.putExtras(params);
    startActivity(i);
}

Activity 2 (reception desk)

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

    Intent i = getIntent();
        if (i != null){
            Bundle params = i.getExtras();

            if (params != null){
                String mScore = params.getString("mScore");

                }

        }

}

  • If what you passed on Intent was an int with params.putInt() then in the other Activity you have to use params.getInt().

  • @ramaral, thanks for the feedback, but even trying what you said and the posts that are flagged as dubbed is not working.

  • I already answered the question in the first comment, I don’t know what else to say. In the accepted answer to the given question you have an example of how to pass an integer.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.