Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to pass the data to sub-activities?

The main activity includes some variables with set values. I created a sub-activity with the form which has to be filled with the data from main activity so I guess the data have to be passed to the sub-activity when it starts.

Does anyone know how to pass the variable values to the sub-activity from the main activity?

Thanks!

like image 257
Niko Gamulin Avatar asked Jul 02 '09 08:07

Niko Gamulin


People also ask

How will you pass data to sub activities in android?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How do you pass the data between two activities?

putExtra() : This method sends the data to another activity and in parameter, we have to pass key-value pair. Add the below code in onClick() method. intent. putExtra("full_name", fullName);

How pass data from one activity to third activity?

You can pass the value in 2 ways: Either you make a global Class and set the value in that class and access that class in your 3rd activity. You can use Intent to send your values from 1st activity to 2nd activity.


1 Answers

You can use this method in your main activity

Intent i = new Intent(this, YourMainClass.class);
i.putExtra("key", value);

end then in the sub activity get the value with this method, usually in the onCreate event

int value = getIntent().getExtras().getInt("key");

I hope this hepls.

like image 148
tdelev Avatar answered Oct 20 '22 20:10

tdelev