I'm making an apps and I need to pass variables between two different activites. I got a TextEdit, in which I save the entry to pass it to another activity. Here's my code :
Activity "textEdit" (let call it like this...) :
if (editText_descriptionHomework.getText().toString().matches("")){
Toast.makeText(getApplicationContext(), messErr_noInput, Toast.LENGTH_LONG).show();
}
else {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
String descr = editText_descriptionHomework.getText().toString();
i.putExtra(description, descr);
finish();
startActivity(i);
}
And here's the "main activity" (in which I want to display this data) :
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("description");
Toast.makeText(getApplicationContext(), value, Toast.LENGTH_LONG).show();
}
So, if I put nothing, I correctly have the toast saying "error blablabl", but if I put something, and I click on the button which trigg the "textEdit activity" code, it display nothing (black toast with no caracters).
Did I write something wrong guys ?
It should be:
i.putExtra("description", descr);
Yours doesn't have the quotes...
first it is better to replace this :
if (editText_descriptionHomework.getText().toString().matches(""))
by
if (editText_descriptionHomework.getText().toString().idEmpty())
that way you are making sure the EditText is empty, .match is like preg_match but for android
Now to get the value, try this instead:
String value = getIntent().getExtrasString("description");
or
String value = getIntent().getExtras().getString("description");
Last replace :
i.putExtra(description, descr);
by
i.putExtra("description",descr);
it should be like this
i.putString("description", descr);
you forgot the quotes
Related
So i have intent that require some extras,but i was thinking is it possible to open it without passing extras and not getting null pointer?
EDIT: I have activity A and activity B. When i press button im passing few strings as extras to activity B. If its not passed,I will get null pointer and app will crash. What i want is, is it possible to check if there is extras passed,and if its passed to get them,if its not passed continue without it and not get null pointer error?
Yes, it is possible to check it. You need to use Intent.hasExtra(String name) where name is the name of the variable you want to check whether it was passed or not. For example: you place inside Intent string called firstName and then you want to use it in another activity. So here's the code you should place in the second activity:
if(Intent.hasExtra("firstName")) //Intent.hasExtra() return true or false
{
String firstName = Intent.getStringExtra("firstName");
//place here code you want to execute if intent contains firstName
}
else
{
//place here code you want to execute if intent does not contain firstName
}
Related question: Check if extras are set or not
Try this code
in A.java:
Intent intent=new Intent(A.this,B.class);
intent.putExtra("data","my custom data here");
startActivity(intent);
in B.java:
String data;
if (getIntent().hasExtra("data") && getIntent().getStringExtra("data")!=null){ //add a check like that
data=getIntent().getStringExtra("data");
}else{
//add your condition when data parameter is not there or null
}
I have an Activity called searchProcedures which allows a user to select from a listview of medical procedures. I navigate to this activity from two other activities called searchHome and describeVisit. I needed a way for searchProcedures to know which activity it should navigate back to onClick. So I pass an intent.extra from searchHome or describeVisit (key:"sentFrom" value""). Then in searchProcedures I use the following code to determine which class to navigate to.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(!extras.isEmpty()){
if(extras.containsKey("sentFrom")){
if(extras.getString("sentFrom") == "searchHome"){
returnIntent = new Intent(searchProcedures.this, searchHome.class);
}
else if(extras.getString("sentFrom") == "describeVisit"){
returnIntent = new Intent(searchProcedures.this, describeVisit.class);
}
else{
Log.d("failed", "the value of getString is " + extras.getString("sentFrom"));
}
}
}
Checking the Log values, the correct values are being passed to and from activity, but when I check extras.getString("sentFrom") == "searchHome/describeVisit" it comes back as false, and returnIntent remains un-initialized. I have tried putting .toString after the .getString to no avail.
1.
== compares the object references, not the content
You should use:
"searchHome".equals(extras.getString("sentFrom"))
Remeber to check blank space,...
2.
You can use a static variable in your SearchProceduresActivity to check where it comes from
SearchProceduresActivity
public static int sFrom = SEARCHHOME;
SearchHomeActivity:
Intent myIntent = new Intent(SearchHomeActivity.this, SearchProceduresActivity.class);
SearchProceduresActivity.sFrom = SEARCHHOME;
startActivity(myIntent);
DescribeVisitActivity:
Intent myIntent = new Intent(DescribeVisitActivity.this, SearchProceduresActivity.class);
SearchProceduresActivity.sFrom = DESCRIBEVISIT;
startActivity(myIntent);
SEARCHHOME, DESCRIBEVISIT value is up to you
Hope this help!
String compare should use equal not ===
have total 3 activites. I pass the data from the first activity like this:
Intent intent = new Intent(getActivity(), Movie_rev_fulldis_activity.class);
intent.putExtra("mov_pos", position + "");
startActivity(intent);
this working fine all data visible to my second activity but i want to display
one filed item to third activity when i click second activity image
here my second activity
youtube_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent youtube= new Intent(getApplicationContext(),PlayYouTube1st.class);
youtube.putExtra("youtubeLink",youtubeLink);
startActivity(youtube);
// Toast.makeText(Movie_rev_fulldis_activity.this,
// youtubeLink, Toast.LENGTH_LONG).show();
}
});
youtubeLink=Reviews_update.revData.get(mov_pos).getYoutubeLink();
Toast.makeText(Movie_rev_fulldis_activity.this,
Reviews_update.revData.get(mov_pos).getYoutubeLink(), Toast.LENGTH_LONG).show();
mov_pos=Integer.parseInt((getIntent().getExtras()).getString("mov_pos"));
in second activity i am getting values but when i send one filed to third activity that value passing null any one please help me i stuck in their,
I want to show YoutubeLink field sencnd activity to third activity how to parse that any one please help
In FastActivity:
Intent in = new Intent(FastActivity.this, SecondActivity.class);
in.putExtra("a", yourdata);
startActivity(in);
In SecondActivity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String a = extras.getString("a");
}
You can also use any static variable and use it in any class
static int a = 5;
And in any other class
int b = classname.a;
classname is the class where static variable is declared.
Make that variable static and then pass
Your variable object reference get change in the THIRD Activity so, it returns null
I am creating an application, surprising I know, and using a quick back button to return the user to a previous listing. The code below is the intent portion that starts the activity. Now it is sending the activity back to itself with an "LvPos" variable to determine which position it just re-start itself at.
Spinner spinMe = (Spinner)findViewById(R.id.spinner1);
Intent backIntent = new Intent(null, null, getBaseContext(), MainActivity.class);
int itemSelected = spinMe.getSelectedItemPosition();
backIntent.putExtra("LvPos", itemSelected);
startActivity(backIntent);
Now the code below is the reference in the onCreate method that gets teh LvPos variable. The problem is, when I get to this portion, the LvPos is null. I have the same code for various other intents and all work fine. If anyone can see any glaring issues, let me know as I have to be severely overlooking something.
int positionID = 0;
Bundle extras = getIntent().getExtras();
if (extras != null){
String LvPosBundle = extras.getString("LvPos");
if (LvPosBundle != null)
positionID = Integer.parseInt(LvPosBundle);
}
Thank you in advance.
You are Using the putExtra(String, int) when you are putting the extra.
When retrieving you use:
extras.getString("LvPos");
instead use:
extras.getInt("LvPos");
Store that in an integer instead of string. Then you also don't have to do the parseInt.
Hope this Helps.
-Travis
I am currently attempting to pass a string across activities. The string is used to set an image in the second activity. So the user is clicking a button in the first activity and then the appropriate image is loaded in the second activity based upon what was selected. There are three buttons in the first activity all which should send a String value to the next activity.
This is what I have in my first activity:
Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String selection = "archer";
Bundle basket = new Bundle();
basket.putString("key", selection);
Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
i.putExtras(basket);
startActivity(i);
finish();
}
});
}
The receiving activity has the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.level_one);
ImageView img = (ImageView) findViewById(R.id.Character_Chosen);
Bundle extras = getIntent().getExtras();
String Toon = extras.getString("key");
if(Toon=="archer"){
img.setImageResource(R.drawable.archer);
}
if(Toon=="mage"){
img.setImageResource(R.drawable.mage);
}
if(Toon=="warrior"){
img.setImageResource(R.drawable.warrior);
}
}
I know that the receive if statements section works because if I manually set the value Toon the image will load. So somewhere the information is not being sent or read properly. I followed a tutorial online and but am stumped on this.
issue is Java 101. string equality test is done using the equals method.
Toon.equals("archer");
Extra tip : inverse your test to avoid null pointer exception :
"archer".equals(Toon);
Extra extra tip : considering your cases are mutually exclusive, use if else if to avoid testing all cases.
You are comparing your strings wrong,
Toon=="Anything"
will never be true, you need to use
Toon.equals("Anything")
for it to work.
EDIT:
This is true because "==" will just compare the memory addresses of the Strings, and "Anything" will just have been allocated and definitely won't match Toon. .equals however actually compares the chars in the string.
Also to test things like this try putting in a
Log.i("YourAppName", "Toon value is: " + Toon);
after
String Toon = extras.getString("key");
try to use SharedPreferences, Set it after/with the button-click and get it on 2. Activity OnCreate
If you just want to pass a simple String, you can use the following:
On sending:
Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
i.putExtra("key", selection);
startActivity(i);
On receiving:
String selection = getIntent().getStringExtra("key");
If there is no parameter with this key, selectionwill be null.
Try this
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("email", "lol#hotmail.com");
startActivity(i);
and
Intent i = getIntent();
String myemail = i.getStringExtra("email");
Furthermore, to compare String, use .equals() not ==