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).
Related
I am trying to pass data from adapter to activity through bundle.
I am checking a checkbox
checkboxSelectSubCategory.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
productModelsAL.get(getAdapterPosition()).setCategorySelected(true);
}else {
productModelsAL.get(getAdapterPosition()).setCategorySelected(false);
}
}
});
and setting data correctly in the object as I have debugged:
Intent intent = new Intent(mContext, SelectProductActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("productModel", productModelsAL.get(getAdapterPosition()));
intent.putExtras(bundle);
mContext.startActivity(intent);
But in activity I am not receiving the boolean value which was passed.
in activitie's onCreate:
Intent intent = getIntent();
if (intent != null) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
subCategory = (SubCategory) bundle.getParcelable("productModel");
}
}
allProductsSelected = subCategory.isCategorySelected();
Why I am not getting the value I am passing??
Actually, my Model class was incorrect.
I had model class n all implemented correctly first.
But for the extra functionality I included the checkbox in the model and created setter getters. But didn't updated the required methods for Parcelable.
Solution:
I deleted the three auto generated methods for Parcelable.
Constructor
Creator
writeToParcel
and recreated them with Alt+Enter.
That's it.
It was bit ignorant and I wasted lot of time with it. Just thought it might help somebody.
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 ===
I have this if code in my activity:
Bundle bundle = getIntent().getExtras();
if (bundle.getBoolean("fourthSection")){
mTitle = getString(R.string.title_section4);
this.setContentView(R.layout.stats);
new uq().execute();
otherLayout = true;
}
How can I get the name of the activity that sends the intent, and to apply the code above only when the first activity's name is equal to "Profile"?
Single Caller: For Example: You just want to call from your Profile class then add an boolean which indicates that it's from Profile class.
Multiple Caller: For Example: There are multiple callers then add an int for identifying from which calls it's been called.
Implementation
In Profile class
Intent intent = new Intent(this, SOME_ACITIVITY.class)
intent.putExtra(ANY_KEY, true); // Set it true for Profile class
startActivity(intent);
Another or Destination class
Write the below code in onCreate() method
if (getIntent() != null) {
boolean callFromProfile = getIntent().getBooleanExtra(ANY_KEY, false);
if (callFromProfile) {
// Write your logic here as we are called from Profile Activity
}
}
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.
This is what i want to do. When a user starts the game, he is taken to a dashboard screen with several categories that he is suppossed to choose from. On clicking any of the categories one is lead to to the a single activity where i need to know how to find which category button was choosen in the ActivityB.
So, in the DashboardActivity, i have this code:
public void onGeneralKnowledgeClick(View v) {
createIntent("GENERAL_KNOWLEDGE", 1);
}
........
........
........
public void onCelebritiesClick(View v) {
createIntent("CELEBRITIES", 6);
}
private void createIntent(String category, int result) {
Bundle bundle = new Bundle();
bundle.putInt(category, result);
Intent intent = new Intent(this.getApplicationContext(),
QuestionActivity.class);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
Now, in my QuestionActivity class, i have a method that tries to get the categoryNumber associated with the intent that was started. So, i have something like this:
private int getCategory() {
Bundle bundle = this.getIntent().getExtras();
int categ = bundle.getInt("GENERAL_KNOWLEDGE");
return categ;
}
My problem is that, how do i return the integer category such and not hardcording as i did up here. I want this method to return a the integer from the respective bundle. my idea is to have a switch statement inside the getCategory but i don't know what case value to use. Also, i saw someone saying that onActivityResult can be used but i don't see how.
Please help!
I believe you're a bit confused. You merely need to do:
bundle.putInt("CATEGORY", result); //where result == the categoryId and "CATEGORY" is always "CATEGORY"
Then in your getCategory():
int categ = bundle.getInt("CATEGORY") //you'll get the int value that you fed before
Unless I misunderstood.