Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

Will this work in the main activity?

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

Followed by:

String value = getIntent().getExtras().getString("key");

And can you add multiple "Extras" like or something like this?

i.putExtra("key", value1); 
i.putExtra("key2", value2);
i.putExtra("key3", value3);

Thanks...

Try this it will work:

activity1.class:

Intent i = new Intent(activity1.this,activity2.class);

Bundle b = new Bundle();
b.putString("name", "your value need to pass here");

i.putExtras(b);
startActivity(i);

activity2.class:

Bundle b = this.getIntent().getExtras();

String name = b.getString("name");

((TextView)findViewById(R.id.textView1)).setText(name);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top