Is it possible to open intent that require extras without passing it? - android

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
}

Related

Determine if data was sent via intent

Is there any way to determine if a specific boolean value was sent via an Intent? In other words, I know how to send data via an Intent and how to read it back, but I would like to know if data was sent.
The current getBooleanExtra method requires a default value, so I can't check if it wasn't sent by using this.
I currently have this:
showNavigationDrawer = getIntent().getBooleanExtra(Extras.EXTRA_SHOW_NAVIGATION_DRAWER, false);
If the Extras.EXTRA_SHOW_NAVIGATION_DRAWER value wasn't set at all, I'd like to do some extra work. Obviously if I get true it means it was sent, however if I get false there's no way to tell.
This can be done extracting the intent bundle:
Bundle b = getIntent().getExtras();
boolean hasNavDrawerSetting = b.containsKey(Extras.EXTRA_SHOW_NAVIGATION_DRAWER);
if (hasNavDrawerSetting) {
showNavigationDrawer = getIntent().getBooleanExtra(Extras.EXTRA_SHOW_NAVIGATION_DRAWER, false);
} else {
showNavigationDrawer = getResources().getBoolean(R.bool.hasSideMenu);
}
If you really want to check whether your value is coming or not then you can take help of Bundle object. You can pass your boolean value through a bundle object. If the bundle object is null then no value is received in the next activity. But if it is not null then you will surely receive the value of your boolean parameter only if you put it in the bundle. It may be true or false depending upon the value you set. Below I'm providing two code snippets. One is for calling activity and another is one for called activity.
Calling Activity -->
Intent intent = new Intent(this,Experimental.class);
Bundle b = new Bundle();
b.putBoolean("key",true);
startActivity(intent.putExtra("bundle",b));
Called Activity -->
setContentView(R.layout.experimental);
Bundle b = getIntent().getBundleExtra("bundle");
if(b != null){
Log.d("Value",String.valueOf(b.getBoolean("key",false)));
}
Hope I'm able to help you.

Android: Passing data from one activity to another activity in ListView

I am making a Grocery list app.
In my first Activity I have a ListView and in my second activity I can add the new grocery details. I want to show those details in the first Activity's ListView. Now how can I pass data from second Activity to the first Activity in ListView.
In second activity i passed data with fooling code:
EditText editName = (EditText) findViewById(R.id.txtName);
EditText editQty=(EditText) findViewById(R.id.txtqty);
String name= editName.getText().toString();
String quantity=editQty.getText().toString();
Intent returnIntent = new Intent();
returnIntent.putExtra("name",name);
returnIntent.putExtra("quantity",quantity);
setResult(RESULT_OK,returnIntent);
finish();
And in first activity i used that Intent as follows:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
Bundle b = data.getExtras();
if ( b!= null ){
String strName=data.getStringExtra("name");
String strQty=data.getStringExtra("quantity");
System.out.println(strName);
System.out.println(strQty);
}
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
Please help me solve this. Thanks in advance.
I suggest you to use SQLite for your issue, when you insert new grocery details, just create one table and insert that details in database table. Now , when you want to display that newely added details, just run "select" query and set your ListView Adapter.
If everytime you do not want to set your adapter, then just maintain one boolean static variable like isGroceryAdded. If grocery details added successfully, at that time make that boolean variable "true". And check in your first activity that is that variable is true, then run query again and set your adapter again, after setting the adapter, just make that variable false again.
Without having some code, my suggestion would be to create a model class that implements serializable, put that in the intent as a result and pass it back to the first Activity (note for this to work, the second activity has to be started with startActivityForResult()).
For a similar question, please have a look at https://stackoverflow.com/a/14333555/1082942
This is a general overview of one solution that should work in most cases. Considering you didn't provide code specific to your situation this may or may not be what you need.
1) To resume the first activity, send an intent from the second activity to the first activity containing the grocery details as intent extras (key-value pairs).
2) Override the first activity's onResume() method to handle the incoming intent to pull out the data you want.
3) Then you need to update the data used by your adapter with this intent extra information.
4) Then you need to call notifyDataSetChanged() on your adapter so it will recreate its views with the updated info
Hope you find this helpful.

Check if Activity is Started with an Intent and If String Extra is passed in

I want an activity to be started only from another Activity, so I am trying to check if this activity is started from that Activity. If not I want to disable the fields and pop up an alert. However no matter how I try to check for this, I get a NullPointerException
Here is my attempt.
Intent intent = this.getIntent();
if (intent != null && intent.getStringExtra("uid").equals(null)) {
showAlert();
disableFields();
} else {
mUID = getIntent().getStringExtra("uid");
}
How can I refactor this to pass?
Here is my NPE
intent.getStringExtra("uid").equals(null) won't save you from a NullPointerException, since you'll be calling a method on a null reference if there's not extra with key "uid". You can use the hasExtra() method to check whether that extra is passed with the Intent.
IN case ur string is null edit ur if condition as
if(intent != null && intent.getStringExtra("uid")!=null)

Passing String from one class to another in Android

I want to transfer a String value from one class to another class in same package. The second class is called using intent in onclick of a menu item. I used the code
Intent i = new Intent(TouristGuideActivity.this, PointOfInterest.class);
i.putExtra("videoId", videoId);
startActivity(i);
in first class and then in second class,
String address=getIntent().getExtras().getString("videoId");`
But when I click on the menu item, I get a force close. If I remove that put Extra part, it works ine. But in that case I can't send the string. Please help!
Intent intent=new Intent(this,secondclass.class);
intent.putExtra("videoId",videoId);
startActivity(intent);
and in your second class try 2 get these items by doing
Bundle extras = getIntent().getExtras();
if(extras!=null){
String videoId=extras.getString("videoId");
}
just after u define your class i hope dis would help u out 2 get the values on other page
Make sure that you are getting the same type, for example, if you put a String, get a String.
Then try with:
getIntent().getStringExtra("videoId");

Intent on Android

I want a clarification on intent.this is my scenario,there are 3 activities in my project namely "A","B","C".....
On clicking the button in Activity "A" ,i pass the values to "B" through bundle and based on the received value i retrieved set of values from database...Similarly "B"->"C" also...
The problem when i clicked the back button in "C" it does not proceed to "B" rather throws a error.similarly "B" to "A" also....
i found the reason that "B" can not get value from bundle when clicking Back button from "C"
Hope you understand.what's the solution....
Thanks in advance
Try starting our Activity using startActivityForResult and use setResult(int) to send the result back from Activity C to B
that true you saying bundle getting null values, so you have to prevent that null values,
like first check bundle values if not Equals null then only set
Intent myintent = getIntent();
if (null != myintent.getExtras()) {
// do your work here
String Name = myintent.getExtras().getString("Name");
} else {
// not here you can't get values
// so getting force close here
Toast.makeText(getApplicationContext(), "No Recor Here..", 12).show();
}

Categories

Resources