Custom RecyclerAdapter and startActivityForResult

3.2k views Asked by At

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

    }
2

There are 2 answers

0
Mark On BEST ANSWER

OK, so IF by any chance somebody else has this problem here is my

solution. I added this code into the MainActivity onActivityResult (note I have a frame container which is where all fragments are inflated):

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // get current fragment in container
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameContainer);
        fragment.onActivityResult(requestCode, resultCode, data);
    }

I believe this works as the MainActivity is top in the hierarchy and intercepts the onActivityResult, so basically I just point it where I want it to be used.

1
Cody Caughlan On

Your calling Activities onActivityResult will only be called when the second activity finishes and a setResult has been executed.

Since the user is hitting the back button the 2nd activity is finishing without setResult being called.

You'll need to override onBackPressed so you can execute your setResult code.

I see you have implemented this but I think the crux of the issue is that you need to return Activity.RESULT_OK not your request code.

@Override
public void onBackPressed() {       
   Intent intent = new Intent();
   setResult(RESULT_OK, intent);

   super.onBackPressed();
}

In this case you don't need to explicitly return your requestCode of Constants.NEW_ITEMS_REQUEST_CODE because Android will forward that automatically.