0

How do you send data from one activity to another using intents.

(Note this is a two-part question, the sending and the receiving).

I'm creating a form, and I want to save the answers to every question in an sqlite database.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
nope
  • 223
  • 2
  • 14

5 Answers5

1

Sending (Activity 1):

Intent intent = new Intent(MyCurentActivity.this, SecondActivity.class);
intent.putExtra("key", "enter value here");
startActivity(intent); 

Receiving (Activity 2):

String text = getIntent().getStringExtra("key");
Indrek Kõue
  • 6,288
  • 8
  • 33
  • 67
1

You can pass data easily using Bundle.

Bundle b=new Bundle();
b.putString("key", value);

Intent intent=new Intent(TopicListController.this,UnitConverter.class);
intent.putExtras(b);

startActivity(intent);

You can recieve data in your other activity like this as follow:

Bundle b=this.getIntent().getExtras();
String s=b.getString("select");
Indrek Kõue
  • 6,288
  • 8
  • 33
  • 67
Android Killer
  • 17,747
  • 12
  • 64
  • 88
0

To send from the Activity

Intent myIntent = new Intent(First.this, Second.class);
    myIntent.putExtra("name","My name is");
    startActivity(myIntent);

To retrieve in the secod

Bundle bundle = getIntent().getExtras();
           String name=bundle.getString("name");
Rasel
  • 16,321
  • 6
  • 41
  • 51
0
Intent i = new Intent(yourActivity.this, nextActvity.class);

i.putExtra("dataName", data);

//recieving

Intent i = nextActivity.this.getIntent();


 string s =  i.getStringExtra("dataName", defaultValue);
ngesh
  • 13,241
  • 4
  • 43
  • 59
0

To pass the data using intent from the current activity use the following code

             Intent in = new Intent(YourClassContext, nextActivity.class);
             in.putExtra("name","value");
             startActivity(in);

To get the intent data passed in the next activity use the following code

     Intent i = nextActivity.this.getIntent();
     String intentDatapassed = i.getStringExtra("name", defaultValue);
bHaRaTh
  • 3,374
  • 4
  • 33
  • 32