How to start a new activity on button click

We can start activity in Android by below code

Intent myIntent = new Intent(CurrentActivity.this, SecondActivity.class);
startActivity(myIntent);

 

We can pass data between activities with Intents.

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

 

How to fetch data from Intent?

On the second activity we can fetch passed data from Previous activity by

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

 

Intents will store data of below types

Integer

String

Boolean

Array

Long

char

Serializable


Subscribe For Daily Updates

Flutter Questions
Android Questions