Custom RecyclerAdapter and startActivityForResult
Asked Answered
B

2

2

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

    }
Balenciaga answered 23/6, 2015 at 18:6 Comment(5)
you need send onActivityResult directly to MyListsFragment from parent of MyListsFragment (activity)Embower
I have edited question as I think it what you mean.Balenciaga
almost done:) add fragment.onActivityResult(requestCode,resultCode,data); in body of method after super.onActivityResult(requestCode, resultCode, data); when fragment is an instance of MyListsFragmentEmbower
Hi, many thanks - this does indeed work :) however whenever I try and execute the method populateRecyclerView(); I get NullPointerException - I'm assuming this is because I added: MyListsFragment fragment = new MyListsFragment(); fragment.onActivityResult(requestCode, resultCode, data);Balenciaga
ok - sorted it - I now understood what you meant - I've included the solution - many thanks for your help +1 !Balenciaga
B
1

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.

Balenciaga answered 23/6, 2015 at 19:57 Comment(0)
P
1

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.

Phare answered 23/6, 2015 at 18:12 Comment(1)
Hi thanks for your advice - I have tried this however it is still not called. I already have onActivityResult being called correctly using my onBackPressed() methos code. I use it in another instance from the first Fragment (not RecyclerAdapter) so i know that onActivityResult is being called when initiated from the first Fragment using startActivityForResult. I just can't get it to work inside the adapter. I think its because its not the initiating fragment, even though I have passed a context.Balenciaga
B
1

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.

Balenciaga answered 23/6, 2015 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.