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
Related
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", "");
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.
In my onCreate() I have:
if(sharedPref.getString("name",null)==""){EditText.setText("Something");}
else{EditText.setText(sharedPref.getString("name",null));}
And then in my onStop() I have:
sharedPrefEditor.putString("name",EditText.getText().toString());
The EditText shows only the hint when I first install and run it. It does seems to display the correct text when it's started later, however.
Don't use == to compare content of Strings. Use equals() instead :
if(sharedPref.getString("name",null).equals("")){
EditText.setText("Something");
}
Why should I use equals instead of ==
First of all you are not using correctly the shared preferences. You are typing null as second parameter which is the value you will get if the key you typed as first parameter is not defined. This is not a problem itself but then in your if sentence you are comparing it to an empty string.
So first of all use equals instead of ==and then if you want to receive an empty string if the key is not defined type an empty string as second parameter of SharedPreferences.gerString method.
Hope it helps ;)
If "Something" is to be used if preference not set, then:
EditText.setText(sharedPref.getString("name","Something"));
getString() will automatically return "Something" if preference is not set. No need of extra code.
String strName = sharedPref.getString("name","");
if(!strName.isEmpty()){
EditText.setText(strName);
}else{
EditText.setText("Something");
}
The operator == can not be used to compare strings in Java.
if(string1.equals(string2){
}
Also, you set a value to SharedPreferences, but you never commit the changes.
sharedPrefEditor.commit();
OnCreate
String prefsVal = sharedPref.getString("name", null);
if(prefsVal.equals(null)){ //If default value was returned
EditText.setText("Something");
}else{
EditText.setText(prefsVal);
}
OR
The following will turn your posted code into a single line solution. The second parameter passed to SharedPref is the default value to be returned if no vale was found for the given key.
EditText.setText(sharedPref.getString("name", "Something");
OnStop
sharedPrefEditor.putString("name", EditText.getText().toString());
sharedPrefEditoy.commit();
In my app i have just imported a string to this activity from another activity.
I need this string to be converted into 5 integers, the following:
int counter1, counter2, counter3, counter4, counter5;
This is my code for receiving the string:
Bundle gotBasket = getIntent().getExtras();
number = gotBasket.getString("lol");
The string is currently passed successfully, all i need is for it to be converted to integers.
Ad.1. Q
Integer.parseInt(gotBasket.getString("lol"));
Watch out for the NumberFormatException - if the string does not contain a parsable integer.
Ad.2. Q
ScrollView per each column? You really want it that way?
Use Integer.parseInt()
EDIT: Or in the first activity attach the extra as an int, then in your new activity use Bundle.getInt()
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)