Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean resets itself to false when getExtra is called

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;
            }
like image 226
Kai Tribble Avatar asked Dec 20 '22 09:12

Kai Tribble


1 Answers

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);
like image 195
Haresh Chhelana Avatar answered Jan 05 '23 13:01

Haresh Chhelana