4

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;
            }
Kai Tribble
  • 208
  • 1
  • 10

2 Answers2

8

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);
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
4

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).

Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
  • Aha! So in future cases just choose one or the other? – Kai Tribble Nov 11 '14 at 09:16
  • Yes. The key thing here is to notice that you're setting a bundle and then reading a value in the bundle when you call `getIntent().getExtras().getBoolean(`, but for that to work, you need to pass a bundle and not a boolean :) – Ben Pearson Nov 11 '14 at 09:18