Search code examples
androidandroid-intentandroid-activitytextviewglobal-variables

How to transfer the value of a int type global variable from one activity to another activity and show it in textview?


/* i have declared a global variable plusMarks and used this variable in the checkAnswer() method and called this method. */

private int plusMarks=0;

  //  private int minusMarks=0;


 //   private int TotalScore;

    @SuppressLint("SetTextI18n")

    private void checkAnswer(boolean userPressed) {

        boolean answerProvided = mQuestionBank[mCurrentIndex].isQuestionTrueAnswer();

        //int messageStringId = 0;



        if (answerProvided == userPressed) {

          //  messageStringId = R.string.correct_toast;

            mGreenTick.setImageResource(R.drawable.green_tick);

            mGreenTick.setVisibility(View.VISIBLE);

            plusMarks++;

        }

        else {

          //  messageStringId = R.string.incorrect_toast;

            mGreenTick.setImageResource(R.drawable.red_cross);

         
   mGreenTick.setVisibility(View.VISIBLE);

          //  minusMarks++;

        }

      //  TotalScore = plusMarks - minusMarks;

        mScoreTextview.setText("Your Score is = " + plusMarks + " Marks out of 10 Marks");


        //  Toast.makeText(MainActivity.this, messageStringId, Toast.LENGTH_SHORT).show();

    }

/* i have called checkAnswer() method in the two buttons i have created an new activity ScoreActivity and i want to transer the value of variable plusMarks in the new activity textview. But unfortunately after many attempts, i am unable to do this, it is showing the value null in my textview. please help me and give solution to this */

  mScoreButton = (Button)findViewById(R.id.score_button);

        mScoreButton.setVisibility(View.INVISIBLE);

        mScoreButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                **Intent intent = new Intent(MainActivity.this, ScoreActivity.class);
                  startActivity(intent);
                  intent.putExtra("PLUS_MARKS", plusMarks);
                  finish();**

            }

        });


This is the new activity i have created


public class ScoreActivity extends AppCompatActivity {

    private TextView mFinalMarks;


    private Button mExitApp;

    private Intent PLUS_MARKS;

    @SuppressLint("SetTextI18n")

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_score);

        mFinalMarks = (TextView)findViewById(R.id.final_marks);

       // mFinalMarks.setText("Final Score is: " + getIntent().getStringExtra("PLUS_MARKS") + "Marks 

out of 10 ");

      //  mFinalMarks.setText(getIntent().getStringExtra("PLUS_MARKS"));

        **mFinalMarks.setText("Final Score is: " + getIntent().getStringExtra("PLUS_MARKS"));**

/*i want to tranfer the value of global variable plusMarks in the new activity textview mFinalMarks. i have tried a lot but it is showing null value. please help me. Also its request to edit the code for better understanding. */


Solution

  • You must put the "extra" in the Intent before you call startActivity(). You have:

    startActivity(intent);
    intent.putExtra("PLUS_MARKS", plusMarks);
    

    You need to do this instead:

    intent.putExtra("PLUS_MARKS", plusMarks);
    startActivity(intent);
    

    Note: Also, you don't have a "global variable". plusMarks is a private member variable of a class. A "global variable" in Java would be declared public static so that it could be accessed directly by any class.