Passing data one activity to another using Intent.
You can use intents, which are messages sent between activities. In a intent you can put all sort of data, String, int,float,double etc.
Here, Before going to activity2
, you will store a String message this way :
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);
Here putExtra() method contain all type of variable. In activity2
, in onCreate()
, you can get the String
message by retrieving a Bundle
(which contains all the messages sent by the calling activity) and call getString()
on it :
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
I am passing string value so i used getString() method if you want to pass int value then use getInt().
Then you can set the text in the TextView
TextView txtView = (TextView) findViewById(R.id.your_resource_textview);
txtView.setText(message);
Hope this helps !