How to Get Extra Data from Intent on Android

How do I get extra data from intent on Android?

First, get the intent which has started your activity using the getIntent() method:

Intent intent = getIntent();

If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:

String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");

How to use putExtra() and getExtra() for string data

Use this to "put" the file...

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String strName = null;
i.putExtra("STRING_I_NEED", strName);

Then, to retrieve the value try something like:

String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

Get string extra from activity Kotlin

Answer found, in the next activity, you have to do this to get the string:

val ss:String = intent.getStringExtra("samplename").toString()

How to retrieve intent extra from a specific intent when two intents lead to the same activity

Add action to differentiate

You can add action to differentiate the different intents

For intent x

Intent x = new Intent(A.this, B.class);
x.setAction("fromActivity");

For intent y

Intent y = new Intent(getContext(),B.class);
y.setAction("fromFragment");

How to differentiate?

String action = getIntent().getAction();
if(action.equals("fromActivity"))
{
// your logic
}
else if(action.equals("fromFragment")){
// your logic
}
else{
// your logic
}

where does the data I passed using putExtra() method of intent to another activity get stored?

1 - If I send data to another activity where does it stored ?

I think there are a few different ways to answer this question, but the most straightforward is: it doesn't matter.

When you use an Intent to send data to another activity, what matters is that the other activity will receive an Intent object that is populated with the data you sent. The exact mechanism of how that happened is something you (as a developer) aren't supposed to care about.

The only exception to this that's worth mentioning is the fact that data in an Intent might (depending on exactly what you're doing) be subject to "Binder transaction size limits"; your data is serialized and transmitted at some point and, if the data is too large, this will fail.

2- what is the difference between put the parameter key of putExtra() method as builtin predefined string like EXTRA_TEXT and put a key with a random name like "mymessage"

There is no technical difference. In fact, Intent.EXTRA_TEXT is defined as a simple string itself ("android.intent.extra.TEXT").

The practical difference is that Intent.EXTRA_TEXT is a well-defined, known value that any developer can use. If I'm writing a chat program, and I want to let other apps open mine and hand off message text, I can tell users that I'll look for Intent.EXTRA_TEXT for this data, and it will be easy for everyone to use.

Within your own app, it really doesn't matter what strings you use for the key in a putExtra() call. As long as you use the same string later on to look it up, you can use anything you want. The only concern here is to make sure that you don't accidentally use the same key for two different values within the same Intent.

3- how does android use the key part to of putExtra method to refer to my data

It doesn't. All the android framework does is take a bunch of key-value pairs, serialize them (if necessary), and transmit them to another activity. That other activity, however, can then use the keys to look up the values. That's why e.g. Intent.EXTRA_TEXT is cool; it's a well-defined key that anyone can use to look data up, counting on callers on the other side to have put something in the map using that known key.

4- how does getExtra() method work from where it get the data

I guess you could say that the data comes from the app's memory.

You can think of the Intent object as being a really fancy Map<String, ?>. The same way you could write String s = (String) map.get("key"), you can write String s = intent.getStringExtra("key").

Passing extra data from an Activity to an Intent

Intent myIntent = new Intent(webPush.this, webPushActivity.class);
myIntent.putExtra("mystring",strValue)' <<---put String here
startActivity(myIntent);

and in second Activity...

String str = getIntent.getExtras().getString("mystring");<<get string in second class

and check this

How do I pass data between Activities in Android application?

Android intent extra data is lost

The problem is the android:launchMode="singleTask"attribute of the receiving task. With this being set, all intents that target the ActivityTo activity are received by the same object. In case the activity was already created, the intent is sent through the activity's onNewIntent(Intent) method. Override it like this:

protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}

This way, you can get the new intent with the getIntent().



Related Topics



Leave a reply



Submit