2

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.

Ju Mo
  • 21
  • 6

2 Answers2

0

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

Noah Mohamed
  • 114
  • 1
  • 1
  • 8
  • Thank you for your reply but that doesn't work because the setResult() and the finish() makes error – Ju Mo Feb 07 '22 at 11:50
  • you donot do that holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.putExtra("editTextValue", "value_here") StructureEnFeuActivity.setResult(RESULT_OK, intent); StructureEnFeuActivity.finish(); } }); you need to use interface fire when any item click and pass any data u want to activity which hold recyclerView and in method you overrided you will call finish() and set data – Noah Mohamed Feb 07 '22 at 11:56
  • I have updated it with that and I have always the same problem – Ju Mo Feb 07 '22 at 12:03
  • of course you have a problem , this implementation i see it for first time ,a don't make sure it work fine, how to call setResult and finish from adapter , as i said you need to use interface as callback fire when any item pressed , so you need to replace Activity object from adapter to an Interface class which will be fire an other side you will implement this class in StructureEnFeuActivity class and override its method inside this method do this work setResult and finish – Noah Mohamed Feb 07 '22 at 12:12
  • I'm sorry but I don't understand how to resolve it – Ju Mo Feb 07 '22 at 12:13
0

After 2 hours in the sauce I have resolve that and I'm very happy. The problem with the setResult() and the finish() but if you apply this code

((StructureEnFeuActivity)context).setResult(RESULT_OK, intent);
((StructureEnFeuActivity)context).finish();

You will not have the probleme with "static". So I'm happy. And thank you for all.

Ju Mo
  • 21
  • 6