How to start new activity on button click

后端 未结 24 1617
傲寒
傲寒 2020-11-21 05:54

In an Android application, how do you start a new activity (GUI) when a button in another activity is clicked, and how do you pass data between these two activities?

24条回答
  •  名媛妹妹
    2020-11-21 06:46

    Easy.

    Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
    myIntent.putExtra("key", value); //Optional parameters
    CurrentActivity.this.startActivity(myIntent);
    

    Extras are retrieved on the other side via:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String value = intent.getStringExtra("key"); //if it's a string you stored.
    }
    

    Don't forget to add your new activity in the AndroidManifest.xml:

    
    

提交回复
热议问题