Newbie
Newbie

Reputation: 75

How to send data from main activity to other activity?

How to send more than 1 data with bundle ?

If only one :

String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);

Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);

a.putExtras(bundle);
startActivityForResult(a, 0);

if more than 1 data ??

String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);

String confirm = txtConfirm.getText().toString();
String txtconfirm = String.valueOf(confirm);

what's next ??

Upvotes: 0

Views: 148

Answers (5)

jinais
jinais

Reputation: 33

The process of serializing/parceling custom objects, attaching to the bundle with keys and undoing all this at the other end gets tedious when you have a lot of data or/and when the data needs to serve different purposes/functions in the launched Activity etc.

You can check out this library(https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher) I wrote to try and solve this problem.

GNLauncher makes sending objects/data to an Activity from another Activity etc as easy as calling a function in the Activity with the required data as parameters. It introduces type safety and removes all the hassles of having to serialize, attaching to the intent using string keys and undoing the same at the other end.

You can also trigger different functionalities in the Activity directly by choosing which method to launch into with the data.

Upvotes: 0

D-Dᴙum
D-Dᴙum

Reputation: 7902

Just put your 2nd string in with bundle.putString() making sure you use a unique key name for it.

Upvotes: 0

Pratik
Pratik

Reputation: 30855

for more than one data

String status = txtStatus.getText().toString();
 String txtstatus = String.valueOf(status);

 String confirm = txtConfirm.getText().toString();
 String txtconfirm = String.valueOf(confirm);

Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
bundle.putString("confirm",txtconfirm);

a.putExtras(bundle);
startActivityForResult(a, 0);

Upvotes: 0

Shlublu
Shlublu

Reputation: 11027

If I understood the question, this should be fine:

Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
bundle.putString("confirm", txtconfirm);

Upvotes: 0

Vineet Shukla
Vineet Shukla

Reputation: 24031

just keep adding in bundle as you are adding bundle.putString("status", txtconfirm );

and when you are done set this bundle to intent:a.putExtras(bundle);

Upvotes: 1

Related Questions