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.
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
}
When I invoke getExtras.getBoolean(key) for my isDeleted boolean, it keeps setting itself to false, even though I'm passing in true. Any insight on why this is occurring? I've tried a lot of other methods, but haven't been successful in keeping the boolean value TRUE.
Other Activity:
public void deleteWorkout(View view)
{
intent.putExtra("listPosition", intent.getExtras().getInt("position"));
intent.putExtra("isDeleted", true);
setResult(RESULT_OK, intent);
finish();
}
Main Activity:
case(List): {
if(resCode == Activity.RESULT_OK)
{
boolean isDeleted = intent.getExtras().getBoolean("isDeleted");
int listPosition = intent.getExtras().getInt("listPosition");
if(isDeleted)
{
adapter.remove(workoutList.get(listPosition));
adapter.notifyDataSetChanged();
}
}
}
default:
break;
}
There is two way pass/get data one activity to another activity.
1.add data to intent.
how to put :
intent.putExtra("listPosition", intent.getExtras().getInt("position"));
intent.putExtra("isDeleted", true);
how to get :
int listPosition = getIntent().getIntExtra("listPosition",0);
boolean isDeleted = getIntent().getBooleanExtra("isDeleted",false);
2.Add data to bundle and add bundle to intent.
how to put :
Bundle bundle = new Bundle();
bundle.putExtra("listPosition", intent.getExtras().getInt("position"));
bundle.putExtra("isDeleted", true);
intent.putExtras(bundle)
how to get :
int listPosition = getIntent().getExtras().getInt("listPosition",0);
boolean isDeleted = getIntent().getExtras().getBoolean("isDeleted",false);
There are two ways you can fix this and understanding why might save you the headache again in the future.
putExtras isn't the same as putExtra.
So what's the difference?
putExtras will expect a bundle to be passed in. When using this method, you'll need to pull the data back out by using:
getIntent().getExtras().getBoolean("isDeleted");
putExtra will expect (in your case) a string name and a boolean value. When using this method, you'll need to pull the data back out by using:
getIntent.getBooleanExtra("isDeleted", false); // false is the default
You're using a mix of the two, which means you're trying to get a boolean value out of a bundle that you haven't actually set, so it's using the default value (false).
I have two activities say X and y. In x there is edittext n 6 radiobutton if user clicks button the value gets retrieved from database based on input from edittext n radiobutton. the values should be displayed in next activity y. can yu pls help in giving snippet..thanks in advance
You can easily pass data from one activity to another using Bundle or Intent.
Lets look at the following example using Bundle:
//creating an intent to call the next activity
Intent i = new Intent("com.example.NextActivity");
Bundle b = new Bundle();
//This is where we put the data, you can basically pass any
//type of data, whether its string, int, array, object
//In this example we put a string
//The param would be a Key and Value, Key would be "Name"
//value would be "John"
b.putString("Name", "John");
//we put the bundle to the Intent
i.putExtra(b);
startActivity(i, 0);
In "NextActivity" you can retrieve the data using the following code:
Bundle b = getIntent().getExtra();
//you retrieve the data using the Key, which is "Name" in our case
String data = b.getString("Name");
How about using only Intent to transfer data. Lets look at the example
Intent i = new Intent("com.example.NextActivity");
int highestScore = 405;
i.putExtra("score", highestScore);
In "NextActivity" you can retrieve the data:
int highestScore = getIntent().getIntExtra("score");
Now you would ask me, whats the difference between Intent and Bundle, they seems like
they do exactly the same thing.
The answer is yes, they both do exactly the same thing. But if you want to transfer alot of data, variables, large array you would need to use Bundle, as they have more methods for transferring large amount of data.(i.e. If your only passing one or two variable then just go with Intent.
You should put the values into a bundle and pass that bundle to the intent that's starting the next activity. Sample code is in the answer to this question: Passing a Bundle on startActivity()?
Bind the data that you would like to send with the Intent you are calling to go to the next activity.
Intent i = new Intent(this, YourNextClass.class);
i.putExtra("yourKey", "yourKeyValue");
startActivity(i);
In YourNextClass activity, you can get the passed data by using
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = extras.getString("yourKey");
}
How to send more than 1 data with bundle ?
If only one :
String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);
Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
a.putExtras(bundle);
startActivityForResult(a, 0);
if more than 1 data ??
String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);
String confirm = txtConfirm.getText().toString();
String txtconfirm = String.valueOf(confirm);
what's next ??
just keep adding in bundle as you are adding bundle.putString("status", txtconfirm );
and when you are done set this bundle to intent:a.putExtras(bundle);
If I understood the question, this should be fine:
Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
bundle.putString("confirm", txtconfirm);
for more than one data
String status = txtStatus.getText().toString();
String txtstatus = String.valueOf(status);
String confirm = txtConfirm.getText().toString();
String txtconfirm = String.valueOf(confirm);
Bundle bundle = new Bundle();
bundle.putString("status", txtstatus);
bundle.putString("confirm",txtconfirm);
a.putExtras(bundle);
startActivityForResult(a, 0);
Just put your 2nd string in with bundle.putString() making sure you use a unique key name for it.
The process of serializing/parceling custom objects, attaching to the bundle with keys and undoing all this at the other end gets tedious when you have a lot of data or/and when the data needs to serve different purposes/functions in the launched Activity etc.
You can check out this library(https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher) I wrote to try and solve this problem.
GNLauncher makes sending objects/data to an Activity from another Activity etc as easy as calling a function in the Activity with the required data as parameters. It introduces type safety and removes all the hassles of having to serialize, attaching to the intent using string keys and undoing the same at the other end.
You can also trigger different functionalities in the Activity directly by choosing which method to launch into with the data.
Android 2.1 update 1
Eclipse 3.5
I have a problem reading data from my 2nd activity that is called from the 1st activity using intent. I have androidmanifest.xml setup correctly.
My First Activity has the following code:
Intent myIntent = new Intent(MainMenu.this, Testmenu.class);
myIntent.putExtra("com.tweaktool.MyAge",40);
myIntent.putExtra("com.tweaktool.Enabled", false);
startActivity(myIntent);
My 2nd Activity has the following code:
Bundle bun = getIntent().getExtras();
int myAge = bun.getInt("MyAge");
boolean enabled = bun.getBoolean("Enabled");
When I look at the above code in 2nd Activity it lists the following:
enabled = false
myAge = 0
Why is this doing this??? Am I doing something simple wrong??
You're putting data with one keys ("com.tweaktool.MyAge", "com.tweaktool.Enabled") and trying to get it with others ("MyAge", "Enabled") -- the bundle then just returns you defaults (0, false). To get what you've put, use the keys you've used.
Have your tried
int myAge = bun.getInt("com.tweaktool.MyAge");?