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,753
  • 72
  • 57
  • 129
nope
  • 223
  • 4
  • 15

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,449
  • 8
  • 37
  • 69
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,449
  • 8
  • 37
  • 69
Android Killer
  • 18,174
  • 13
  • 67
  • 90
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
  • 15,499
  • 6
  • 40
  • 50
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,398
  • 4
  • 44
  • 60
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,464
  • 4
  • 34
  • 32