Given two activities, MainActivity and DetailActivity, what is the necessary code to start the DetailActivity from MainActivity and send 2 parameters, one string and the other an integer, and how can you access the parameters in the newly started activity?
Asked
Active
Viewed 451 times
-3
-
Can you explain to readers what you have discovered, by searching the internet and reading the manual? – halfer Jun 21 '20 at 11:38
-
This is a duplicate https://stackoverflow.com/questions/3510649/how-to-pass-a-value-from-one-activity-to-another-in-android#:~:text=Standard%20way%20of%20passing%20data%20from%20one%20activity%20to%20another%3A&text=putString(%E2%80%9CONE%E2%80%9D%2C%20one,getExtra()%20to%20get%20data. – Shalu T D Jun 21 '20 at 11:45
-
Does this answer your question? [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Shalu T D Jun 21 '20 at 11:46
-
Did you use putExtra and getExtra? – CleanCoder Jun 21 '20 at 11:49
-
1Please don't invalidate your question by mutilating it. If you want to thank people and signal that your question has been answered, accept the answer that most helped you. I have rolled back your edit. I recommend you familiarize yourself with how Stack Overflow works by taking the tour and reading the help center. – Mark Rotteveel Jun 21 '20 at 12:57
3 Answers
1
The easiest way to do this would be:
Intent intent = new Intent(getBaseContext(), DetailActivity.class);
intent.putExtra("MY_STRING", "hello");
intent.putExtra("MY_INT", 42);
startActivity(intent);
And to retrieve it:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myString = getIntent().getStringExtra("MY_STRING");
int myInt = getIntent().getIntExtra("MY_INT", 0); // 0 = default value
// your code
}

Enzo Caceres
- 519
- 3
- 15
0
First you need to create a new activity.
Then create an Intent in the main activity from where you want to move to the next one :
Intent intent=new Intent(getApplicationContext(),DetailActivity.class);
then send the parameters you want to send using:
intent.putExtra("name",name);
intent.putExtra("age",age);
then start the activity
startActivity(intent);
Go the the detailActivity and put this:
Intent intent=getIntent();
String name=intent.getStringExtra("name");
Integer age=intent.getStringExtra("age");

Anshuman singh
- 11
- 2
-
Intent intent = new Intent(MainActivity.this, DetailActivity.class); intent.putExtra("name", "John"); intent.putExtra("age", 22); – Johny Jun 21 '20 at 11:52
-
Intent intent = getIntent(); String suburb = intent.getStringExtra("name"); Integer age = intent.getIntExtra("age"); – Johny Jun 21 '20 at 11:53