I have a Fragment
that contains a RecyclerView
which uses a custom RecyclerAdapter
. I have an onClickListener
inside my custom RecyclerAdapter
- when a position is clicked I want it to start startActivityForResult
. So far this works in as such as when it is clicked it starts the Activity as desired. However when I press the back button to go to the Fragment
containing the RecyclerView
onActivityResult
is never called. I have passed in a context to the custom RecyclerAdapter
. Is this something that is possible? Or does the Activity/Fragment initiating startActivityForResult
be the one that intercepts it? If not I will end up handling the onClick in the Fragment with a gesture detector or something similar, but before that I wanted to give this a fair crack! Note: I have included onActivityResult
in the MainActivity
which has the Fragment
container so the Fragment
does receive onActivityResult
if startActivityForResult
is initiated from the Fragment
. My code:
RecyclerAdapter onClickListener:
@Override
public void onClick(View v) {
String titleId = titlesListDataArrayList.get(getLayoutPosition()).getTitle_id();
Intent intent = new Intent(context, CreateItemsActivity.class);
intent.putExtra("TITLE_ID", titleId);
((Activity) context).startActivityForResult(intent, Constants.NEW_ITEMS_REQUEST_CODE);
}
CreateItemsActivity.class - onBackPressed()
@Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(Constants.NEW_ITEMS_REQUEST_CODE, intent);
finish();
}
MyListsFragment.class (contains RecyclerView)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("CALLED", "OnActivity Result");
// if requestCode matches from CreateItemsActivity
if (requestCode == Constants.NEW_ITEMS_REQUEST_CODE) {
Log.e("REQUEST CODE", String.valueOf(requestCode));
populateRecyclerView();
}
}
Also I have this in the MainActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// included to allow fragment to receive onActivityResult
}
onActivityResult
directly toMyListsFragment
from parent ofMyListsFragment
(activity) – Embowerfragment.onActivityResult(requestCode,resultCode,data);
in body of method aftersuper.onActivityResult(requestCode, resultCode, data);
whenfragment
is an instance ofMyListsFragment
– Embower