.putExtra into another file - android

This is my code for the .putExtra:
String url = "test";
startActivity(new Intent(MainActivity.this, RelationshipRemoved.class)
.putExtra("userInfo", urlTwo));
How do I call the value urlTwo in another file?

You can receive the value in another Activity (not just another file) reading the value from the received bundle, like this:
String userInfo = getIntent().getStringExtra("userInfo");
or
String userInfo = getIntent().getExtras().getString("userInfo");
Example:
Activity sending an intent:
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putExtra("userInfo", "abcdef123456");
startActivity(intent);
ActivityTwo receive the data stored :
String receivedValue = getIntent().getStringExtra("userInfo");
the variable receivedValue will contain the string value "abcdef123456".

Related

Intent getStringExtra returning null

I know this question has been asked a lot, but I have no idea where I went wrong..
So I sent some data through one activity and retrieving it from the other activity.
FirstActivity
Timber.i("Photo img : %s", posterUrl);
String url = AppConstants.IMAGE_BASE_URL+
AppConstants.POSTER_SIZE_ORIGINAL+ posterUrl;
Intent i = new Intent(this, ImageFullActivity.class);
i.putExtra(AppConstants.IMAGE_URL, url);
startActivity(i);
Recieving Activity
Intent i = new Intent();
String imageUrl = i.getStringExtra(AppConstants.IMAGE_URL);
Timber.i("GOt image url %s", imageUrl);
Picasso.with(this)
.load(imageUrl)
.into(image);
And I checked. I am not passing null through the extras. The posterUrl is a valid string.
You need to use getIntent() to get the intent instance that started the next activity as
getIntent().getStringExtra
instead of creating a new empty one
//Intent i = new Intent(); remove
Intent i = getIntent(); // or do this
Try this
//Intent i = new Intent();
String imageUrl = getIntent().getStringExtra(AppConstants.IMAGE_URL);
Timber.i("GOt image url %s", imageUrl);
Picasso.with(this)
.load(imageUrl)
.into(image);
OR
Bundle bundle = getIntent().getExtras();
String imageUrl = bundle.getString(AppConstants.IMAGE_URL);
Timber.i("GOt image url %s", imageUrl);
Picasso.with(this)
.load(imageUrl)
.into(image);
String imageUrl = getIntent().getStringExtra(AppConstants.IMAGE_URL);
You need to get the intent by getIntent() method
Problem is that you are trying to create new intent in your receiving activity which does not have your data what you passed in other activity.
To get the intent what you passed in other activity use getIntent().
Intent i = getIntent();
Here this intent will be holding your data you passed from other activity.

Android getting values from intent

Below shown is the code, i am using to get values from the intent
Bundle extras = intent.getExtras();
extras object has the follwoing information
Bundle[{message=Order #400000063 is Ready for pickup, android.support.content.wakelockid=2, collapse_key=do_not_collapse, from=552489062080, e_id=364}]
When I say
extras.getString("message");
returns null. I am not sure how to get the values ?
In your Activity1:
Intent i = new Intent(Activity1.this,Activity2.class);
i.putExtra("message", "string_value");
startActivity(i);
In your Activity2:
// do a try/catch block or check
//if getIntent().getStringExtra("message") is null
String str = getIntent().getStringExtra("string_tag");
Probably you are writing different tag name ..
Use the same tag name which you used while sending data with Intent.
while sending data if you used code as
Intent i = new Intent(Activity1.this,Activity2.class);
i.putExtra("TAG_NAME", "string_value");
startActivity(i);
then use code below as
String str = getIntent().getStringExtra("TAG_NAME");
try it...

Two EXTRA_MESSAGE from Intent

Can i have two extra messages on Intent that i can pass on another Activity? If yes, how does it work?
this is what I did:
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra(EXTRA_MESSAGE, message);
intent3.putExtra(EXTRA_MESSAGE, messages);
startActivity(intent3);`
and on the Activity that will receive the message:
Intent intent3 = getIntent();
String message = intent3.getStringExtra(Browse.EXTRA_MESSAGE);
String messages = intent3.getStringExtra(Browse.EXTRA_MESSAGE);`
You have to change the KEY:
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra(EXTRA_MESSAGE, message);
intent3.putExtra(EXTRA_MESSAGE_TWO, messages);
startActivity(intent3);
Not exactly like that, but it is easy to send multiple values. The first parameter to putExtra or getStringExtra is a key into a map of values so it must be unique. As long as you use a different key for each value you can put as many as you want in the intent.
For example:
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra("name", message);
intent3.putExtra("path", messages);
startActivity(intent3);
Intent intent3 = getIntent();
String message = intent3.getStringExtra("name");
String messages = intent3.getStringExtra("path");`
This is a problem the way you have them. You need separate keys for each value. So for example
intent3.putExtra("msg1", message);
intent3.putExtra("msg2", messages);
then in your receiving Activity you can get them using the above keys. But you can add as many Extra values as you want.
Two different keys and messages will do it
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra(EXTRA_MESSAGE1, message);
intent3.putExtra(EXTRA_MESSAGE2, messages);
startActivity(intent3);

Using intent.Action_call for user inputted number

Help me modify with necessary changes needed to use the value/string(number) passed by user for phone-calling that string itself
Intent phnIntent = new Intent(Intent.ACTION_CALL);
phnIntent.setData(Uri.parse("tel:+91987654321"));
you can use like this :
String strTelNo = "+91987654321";
Intent intent = new Intent("android.intent.action.CALL");
Uri data = Uri.parse("tel:"+ strTelNo );
intent.setData(data);
startActivity(intent);
Try this link.
Make sure you add the necessary permission in the Android Manifest.
<uses-permission android:name="android.permission.CALL_PHONE" />
Just add the line startActivity(phnIntent);
EDIT:
Activity A
Intent someIntent = new Intent(A.this, B.class);
someIntent.putExtra("phoneNumber", number);
startAcitivty(someIntent);
Acitivity B
Bundle extras = getIntent().getExtras();
String number = extras.getInt("phoneNumber");
Use the number string to make the call.

intent.putExtra not working

I need to pass information between two activities, but for some reason the information isn't sent / recieved.
LogCat doesn't give me any errors. The dubugger clearly shows something is added to the intent (variabl: mExtras), but it's hard to interpret exactly what is added. After that it gives me "source not found" and doesn't help me further.
But first things first. Am I doing things right so far?
Sending:
Intent intent = new Intent ( this, TaskListActivity.class );
intent.putExtra ( ProjectManager.ID, mId.toString () );
startActivity ( intent );
Recieving:
Intent intent = getIntent ();
mId = UUID.fromString ( intent.getStringExtra ( ProjectManager.ID ) );
add following code after intent:
Bundle extras = intent.getExtras();
String exampleString = extras.getString(ProjectManager.ID);
what is ProjectManager.ID?, you should pass same uniquekey while recieving data from putExtra even way of receiving data is wrong, check below code:
Sending:
Intent intent = new Intent ( this, TaskListActivity.class );
intent.putExtra (ProjectManager.ID, mId.toString () );
startActivity ( intent );
Recieving:
Bundle extras = intent.getExtras();
if(extras!=null){
String _Str = extras.getString(ProjectManager.ID);
}
Try This to Receive Extra:
Bundle extras = getIntent().getExtras();
String id;
if (extras != null) {
id= extras.getString(key);
}
In FirstActivity.java write these code.
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
i.putExtra("KEY",value);
startActivity(i);
In SecondActivity.java:
Bundle extras=getIntent().getExtras();
String name=extras.getString("key"); //if data you are sending is String.
int i=extras.getInt("key"); //if data you are sending is integer.
To retrieve the extras in the new activity:
String valueOfExtra;
Intent i = getIntent();
//check first
if(i.hasExtra("extra1")){
valueOfExtra = i.getStringExtra("extra1");
}

Categories

Resources