'I want to take the data and close the current layout on the click on the item of recyclerview

I'm stuck by a problem and I don't know how too resolve it. I want to click on my item in my recycler view, and at this click the layout with the recylcer view will be closed, and my mainActivity will be refresh with the data of the item who i have click on.



Solution 1:[1]

you start new activity to get Data but here you start a normal intent you need to start Activity For Result like

startActivityForResult(intent,REQUEST_CODE);

or new Way

    ActivityResultLauncher<Intent> result = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
    @Override
    public void onActivityResult(ActivityResult result) {
        if(result.getResultCode() == RESULT_OK){
            String data =  result.getData().getStringExtra("Data");
            // here your data come from next Activity
        }
    }
});

then call result by

 result.launch(Intent);

and in other Activity activity

you need to call

setResult(RESULT_OK,Intent);
    finish();

this intent hold your data and will return to previous activity with it

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Noah Mohamed