Passing data between 2 activities
I am programming in android for a Samsung tablet, and I have 2 activities, one of this is a list of football teams, and the other activi开发者_开发问答ty is their twitter, but i have a problem to pass the parameter for the first activity to the second one. I want to pass their url like a string, but i cant get it. Thanks!
You have to use Intent:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("extraURL", "http://myUrl.com");
startActivity(i);
Then, to retrieve it in SecondActivity, in the onCreate method do:
Intent receivedIntent = getIntent();
String myUrl = receivedIntent.getStringExtra("extraURL");
You typically launch the 2nd Activity
from the first using an Intent
. You can pass data to the 2nd Activity
using the same Intent
you use to launch it. For example,
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("url", "http://url.you.want.to.pass/");
startActivity(i);
In the 2nd Activity, in the onCreate, you can read the data using:
Intent i = getIntent();
String url = i.getStringExtra("url");
your First Activity on click of button
Intent intent = new Intent(this,ActivityTwo.class );
intent.setAction(intent.ACTION_SEND);
intent.putExtra("www.google.com",true);
startActivity(intent);
Receive it in ActivityTwo:-
Intent intent = getIntent();
String msg = intent.getStringExtra("www.google.com");
精彩评论