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.
Asked
Active
Viewed 53 times
2
-
I think you re-pasted the Activity file in the adapter part of your question. Please update the same. – Sujal Kumar Feb 07 '22 at 10:54
-
My bad thank you – Ju Mo Feb 07 '22 at 11:00
-
So you want to pass back the data from a specific RecyclerView item back to the MainActivity. Did I understand that right? – Sujal Kumar Feb 07 '22 at 11:05
-
@SujalKumar Yes, that it. – Ju Mo Feb 07 '22 at 11:07
-
I think this should help https://stackoverflow.com/a/14292451/9520315 – Sujal Kumar Feb 07 '22 at 11:08
-
I have tried this, but the setResult() and the finish() make an error like "Non-static method 'setResult(int, android.content.Intent)' cannot be referenced from a static context" – Ju Mo Feb 07 '22 at 11:12
-
I'm beginner in computer programming, si I can have make a stupid mistake I don't know. – Ju Mo Feb 07 '22 at 11:13
-
Can you update the code in the question with what you just tried? – Sujal Kumar Feb 07 '22 at 11:19
-
I have updated it. – Ju Mo Feb 07 '22 at 11:48
2 Answers
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
-
-
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
-
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