Sending an intent without a string first parameter - android

Is it possible to send an intent to a second activity without filling out the first string parameter in intent.putExtra(String,var type) ?
Right now I have this:
intent.putExtra("Average Score: ", averageScoreToSend);
I don't want to send the string if I don't have to, but if I have to then I'll just deal with it.
Also how do I pull the double value from the sent intent? I don't understand what value goes in the second parameter
Intent i;
double averageScore = 0;
i = getIntent();
averageScore = i.getDoubleExtra(Grade1.FINALMATHGRADE, IDK);

I don't need to send the string at all if I don't have to
Yes, you do. The extras are a key-value store, and the first parameter is the key. You need the key to look up the value later on.
Also how do I pull the double value from the sent intent?
Call the appropriate get...Extra() method, passing in the key as the first parameter and the default value as the second parameter. The default value will be used in case the Intent does not contain an extra under the designated key.

Related

android putExtra shows null

I am passing data from one activity to another but if I print the data in the other activity, it shows null.
I used PutExtra and GetExtra.
in first acticity
intent.putExtra(pin,pins);
System.out.println(pin);
In second Activity
Intent intent = getIntent();
pined=intent.getStringExtra("pins");
System.out.println(pined);
I got a null message when I try to print the data
2019-09-05 08:59:09.425 26480-26480/com.example.beachbocI/System.out: null
In the first activity
intent.putExtra("pin",pins);
System.out.println(pin);
In the second activity
pined=getIntent().getStringExtra("pin");
System.out.println(pined);
It shoud be like this
intent.putExtra("pin",pins);
//"pin" means the name of string
//pins means your value
To pass value you should write below
intent.putExtra("pins",pin); //here `pins` is key use it to get your desired value
to get your desired value use the same key as below
intent.getStringExtra("pins"); // use same key `pins`
So putExtra has two params, 1st is a key, second value
pin should be String key object
getStringExtra has one param, it takes a String key value, which should be pin

Android TextView display a string to three decimal places through double convertion from another class

I cannot restrict the TextView to show only 3 decimals when I'm using a variable from another class. Here's the problem:
I have this TextView
public static TextView pt_resultado;
However when I tried to use it to show the number inputted from another screen while converting from Double restricting the decimals:
pt_resultado.setText(String.format("%.3f",(Double.toString(ActivityPopulacao.pt_resultado2))));
I get the following error:
java.util.IllegalFormatConversionException: %f can't format java.lang.String arguments
Is there a way to show only three decimals, put without changing the conversion?
You are trying inserting string as float.
You should do:
pt_resultado.setText(String.format("%.3f",Double.valueOf(ActivityPopulacao.pt_resultado2.getText().toString())))
You need to:
Retrieve String from TextView.
Convert String to Double/Float.
Format Double/Float value as String at 3 decimal places.
I assumed that ActivityPopulacao.pt_resultado2 is also TextView!
To help you also understand why this is happening ill break it flow a bit.
double recievedNum = ActivityPopulacao.pt_resultado2;
String convertedNumAsString = String.format ("%.3f", recievedNum);
pt_resultado.setText(convertedNumAsString);
Can you see the mistake? Your using Double.toString(number) where its expecting a number. String.format takes in the double does formatting and converts to string for you.
Defining TextView as a static view will cause memory leaks. Going forward, you can pass value from one activity to another using Intent - I presume you're using activity. Also, getText().toString() has already converted the input value to string. So you don't need that Double.toString() any more. Copy below code and paste it in the code that is responsible for starting the new activity.
Intent in = new Intent(ActivityPopulacao.this, NewActivity.class);
in.putExtra("key", pt_resultado2.getText().toString());
startActivity(in);
To receive the value, get the intent and request for the string in your NewActivity onCreate() method.
String newDouble = getIntent().getStringExtra("key");
pt_resultado.setText(newString);
Note that NewActivity in the above snippet refers to the activity that needs the value.
Why do you convert to string and try to format it like it is a Double?
The format() method expects a number to format.
Since ActivityPopulacao.pt_resultado2 is Double you should format the double value:
pt_resultado.setText(String.format("%.3f", ActivityPopulacao.pt_resultado2));

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

my Question is :
1 - If I send data to another activity where does it stored ?
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"
i.e
what is the difference between this code
public void go(View view)
{
String mytxt="hellow";
Intent i=new Intent(this , SecondActivity.class);
i.putExtra(Intent.EXTRA_TEXT , mytxt);
startActivity(i);
}
and this code
public void go(View view)
{
String mytxt="hellow";
Intent i=new Intent(this , SecondActivity);
i.putExtra("mydata" , mytxt);
startActivity(i);
}
3- how does android use the key part to of putExtra method to refer to my data
4- how does getExtra() method work from where it get the data
I think that 3 ans 4 are relevant by nature to the other above parts but I want to be clear about all my questions
thanks in advance
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").
Your answers are as follow.
1. When we move from one Activity to another Activity, then data is passed using Intent and this data is sent to the system OS for safe keeping for restoration later, when we reach to another activity this data is restored from OS, There is a limit of 1 MB of this data, If It crosses then we get a crash
"TransactionTooLargeException" for nougat+ android devices.
2. Intent.EXTRA_TEXT is basically a predefined string in Intent class.
public static final String EXTRA_TEXT = "android.intent.extra.TEXT";
Google has coded internally for this key, suppose If we want to share a text with an application then we send this predefined key to send that "text" which need to be shared, thus It is a Standard Reserved key for which Google has coded internally to get and utilize that text
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "my sharign text here");
and on the other side, If we send any our key suppose "my_key" then we handle this on the other activity.
3/4.
When you pass on data from Activity 1 using putExtra() method, then all the data sent through putExtra() is packaged into a bundle, We can fetch this whole bundle in Activity2 using getExtras() method, or we can get our separate key value using getStringExtra(), getIntExtra(), getBooleanExtra() etc.
If you send extras to an activity, the extra data is not stored in a physical device like internal/external storage, but it is stored temporarily in RAM.
Intent.EXTRA_TEXT is a constant defined in the class Intent:
public static final String EXTRA_TEXT = "android.intent.extra.TEXT";
it is used with intents like sending emails and not passing data to activities, although it is not prohibited.
(and 4) The key part of the extra data is used to refer to the actual value of the data:
intent.putExtra("key1", "A");
intent.putExtra("key2", "B");
when the activity that opens wants to retrieve the extra values sent to it by the parent activity, it will check the keys:
if(getIntent().hasExtra("key1"))
String value1 = getIntent().getStringExtra("key1", "");

How to send an arbitrary string via an Intent?

I'd like to send arbitrary strings (not paths or URLs) between two of my company's Android apps, but the Intent data is a URI. Presently I'm concerned with starting an Activity, but in some cases it would be nice to be able to send a query and receive a response without starting an Activity. So:
How should I send any arbitrary string when Intent wants a URI?
What other communication mechanisms could I use?
For Intent intent = new Intent();, you can use intent.putExtra(String name, String value); to store a String value that is keyed to name. In your called activity, use intent.getStringExtra(name); to retrieve the value.
You can add an arbitrary string to an intent by using extras on the intent.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(SOME_STATIC_NAME_VALUE, "your_arbitrary_string");
There are many options for extra's, see http://developer.android.com/reference/android/content/Intent.html
To retrieve the string in your receiving activity, use
intent.getExtras().getString(SOME_STATIC_NAME_VALUE)
Or the appropriate getter for whatever value you set on the extra.

How to determine the type of an extra in a bundle held by intent?

I am trying to pass arbitrary data to a BroadcastReceiver through its Intent.
So I might do something like the following
intent.putExtra("Some boolean", false);
intent.putExtra("Some char", 'a');
intent.putExtra("Some String", "But don't know what it will be");
intent.putExtra("Some long", 15134234124125);
And then pass this to the BroadcastReceiver
I want to iterate through Intent.getExtras() with something like keySet(), but I would also like to be able to get the value of the key without having to hard-code calls to methods like .getStringExtra(), or .getBooleanExtra().
How does a person do this?
You can use the following code to get an object of any time from the Intent:
Bundle bundle = intent.getExtras();
Object value = bundle.get("key");
Then you can determine value's real type using Object's methods.
You can browse thye keys without knowing the type of the values using keySet(). It returns you a set of String that you can iterate on (see doc).
But for the values, it is normal that you have to use a typed method (getStringExtra(), getBooleanExtra(), etc): this is caused by the fact Java itself is typed.
If you would like to send data of arbitrary types to your BroadcastReceiver, you should either:
convert all your extras to Strings before sending them, and retrieve all of them as Strings :
intent.putExtra("Some boolean", "false");
intent.putExtra("Some char", "a");
intent.putExtra("Some String", "But don't know what it will be");
intent.putExtra("Some long", "15134234124125");
or use the get() method of the Bundle that returns Objects (see doc):
Object o = bundle.get(key)

Categories

Resources