Can someone please tell me how exactly to use getExtra()
and putExtra()
for intents? Actually I have a string variable, say str, which stores some string data. Now, I want to send this data from one activity to another activity.
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra(strName, keyIdentifer );
and then in the SecondScreen.java
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.table);
TextView userName = (TextView)findViewById(R.id.userName);
Bundle bundle = getIntent().getExtras();
if(bundle.getString("strName")!= null)
{
//TODO here get the string stored in the string variable and do
// setText() on userName
}
}
I know it is very basic question but unfortunately I am stuck here. Please help.
Thanks,
Edit: Here the string which I am trying to pass from one screen to the other is dynamic.
That is I have an editText where I am getting string whatever user types. Then with the help of myEditText.getText().toString()
. I am getting the entered value as a string then I have to pass this data.
This is what i have been using, hopfully it helps someone.. simple and affective.
send data
get data
cheers!
put string first
retrieve it after that
thats All ;)
Android has introduced new methods in Intent class.
hasExtra()
for checking if intent has data on key.getStringExtra()
directly.Pass Data
Get Data
I always put keys in constants for best practice (to avoid manual errors)
first Screen.java
Second Screen.java
It is very easy to implement
intent
in Android.. It takes you to move from one activity to another activity,we have to two methodputExtra();
andgetExtra();
Now I am showing you the example..Now we have to get the value from
AnyKeyName
parameter,the below mentioned code will help in doing thisWe can easily set the receiving value from
Intent
,wherever we required.Use this to "put" the file...
Then, to retrieve the value try something like: