Boolean resets itself to false when getExtra is called

When I call getExtras.getBoolean (key) on my isDeleted boolean, it retains false even though I am passing true. Any insight as to why this is happening? I have tried many other methods but have no success in storing the boolean TRUE.

Other activities:

   public void deleteWorkout(View view)
    {
        intent.putExtra("listPosition", intent.getExtras().getInt("position"));
        intent.putExtra("isDeleted", true);
        setResult(RESULT_OK, intent);
        finish();
    }

      

Primary 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;
            }

      

+3


source to share


2 answers


There are two ways to fix this and understand why it can save you headaches in the future.

putExtras

does not match putExtra

. So what's the difference?

putExtras

expects a packet to be transmitted. When using this method, you will need to pull the data with:

getIntent().getExtras().getBoolean("isDeleted");

      



putExtra will expect (in your case) a string name and boolean. When using this method, you will need to pull the data with:

getIntent.getBooleanExtra("isDeleted", false); // false is the default

      

You are using a combination of the two, which means that you are trying to get a boolean value from a package that you didn't actually set, so it uses the default (false).

+3


source


There are two ways to transfer / receive data from one activity to another activity.

1.add data for intent.

how to put:

intent.putExtra("listPosition", intent.getExtras().getInt("position"));
intent.putExtra("isDeleted", true);

      

how to get the:

int listPosition = getIntent().getIntExtra("listPosition",0);
boolean isDeleted = getIntent().getBooleanExtra("isDeleted",false);

      



2. Add data for grouping and binding to intents.

how to put:

Bundle bundle = new Bundle();
bundle.putExtra("listPosition", intent.getExtras().getInt("position"));
bundle.putExtra("isDeleted", true);
intent.putExtras(bundle)

      

how to get the:

int listPosition = getIntent().getExtras().getInt("listPosition",0);
boolean isDeleted = getIntent().getExtras().getBoolean("isDeleted",false);

      

+4


source







All Articles