简体   繁体   中英

Transfer from one activity to another

This is the code of my mainActivity and I have created another activity called playAgain . when timer finish I want to call playAgain to display my final score and play again button and when I click on play again button, it should transfer to main activity and set time back to 30 seconds and score to 0/0.

//this is the function in mainActivity to call playAgain activity

public void playAgain(){
        score = 0;
        numberOfQuestions = 0;


        timerTextView.setText("30s");
        pointsTextView.setText("0/0");
        resultTextView.setText("");


        new CountDownTimer(30100, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                timerTextView.setText(String.valueOf((millisUntilFinished)/1000) + "s");

            }

            @Override
            public void onFinish() {

                timerTextView.setText("0s");

                String getFinalScore = Integer.toString(score) + "/" + Integer.toString(numberOfQuestions);

                Intent intent = new Intent(getApplicationContext(), PlayAgain.class);

                //Create the bundle
                Bundle bundle = new Bundle();

                //Add your data to bundle
                bundle.putString("points", getFinalScore);

                //Add the bundle to the intent
                intent.putExtras(bundle);

                startActivity(intent);

                //playAgainButton.setVisibility(View.VISIBLE);

               // resultTextView.setText("Your Score: " + Integer.toString(score) + "/" + Integer.toString(numberOfQuestions));



            }
        }.start();

}

//this is code from my playAgainActicity

Button playAgainButton;
TextView finalTextView;

public void playAgain(View view) {

    //Get the bundle
    Bundle bundle = getIntent().getExtras();

    //Extract the data…
    String getFinalScore = bundle.getString("points");

    finalTextView.setText(getFinalScore);

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
}

Move this code:

//Get the bundle
    Bundle bundle = getIntent().getExtras();

    //Extract the data…
    String getFinalScore = bundle.getString("points");

    finalTextView.setText(getFinalScore);

to your playAgainActicity's onCreate method. And in playAgain method add finishAffinity :

public void playAgain(View view) {

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
    finishAffinity();
}

The code of MainActivity looks ok

please change the code of PlayAgain activity as below; and don't forget to add PlayAgain into Manifest file, and use android:onClick="playAgain" for your button.

public class PlayAgain extends AppCompatActivity {

    TextView finalTextView;

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

        //Get the bundle
        Bundle bundle = getIntent().getExtras();

        //Extract the data…
        String getFinalScore = bundle.getString("points");
        finalTextView = findViewById(R.id.finalTextView);

        finalTextView.setText(getFinalScore);

    }


    public void playAgain(View view) {

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);
    }
}

Hope that helps!

In your playAgain activity put this line in onCreate method

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String getFinalScore = bundle.getString("points");

finalTextView.setText(getFinalScore);

call playAgain() on button's click event

playAgainButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        playAgain();
    }
});


public void playAgain() {
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM