How to start a fragment from a RecyclerView Adapter which is inside another fragment?

I have a tab view with two fragments. Those two fragments contain a recycler view with cards. Each card in both fragments had a button. Clicking on fragment 1's button should open the fragment 2 as a separate page and vice-versa. I am struggling to find a method to implement this without making every too complex and tightly coupled.

This is fragment one with its own Adapter. enter image description here

And this is fragment two: enter image description here

Clicking on that SELECT DONOR button in Donees page should open donor fragment in a new page where the user will be able to assign a donor for the selected donee.

So I have two needs here

1) To start a fragment from a fragment

2) To Keep track from which Donee the new donor page was opened so that I can assign a donor for that specific donee.

I hope this is understandable.

so far I have tried LocalBroadcast and FragmentManager but its hard to keep track of what I'm doing with the code.

Can you guys suggest a better technique to achieve this ?

2 answers

  • answered 2017-06-17 17:57 Zhyano


    the easiest solution would probably be, starting a new activity, passing something like an ID, name or something to the intent on an Button click.

    Context.startActivity(new Intent(Context, YourAssigneeActivity.class)
                              .putExtra("ID",id));
    

  • answered 2017-06-17 17:57 Rene Ferrari

    So I assume that you do not switch to the other tab when you click a button on one tab. Therefore the fragment should fill the whole screen.

    With this assumption in mind you most likely have to switch the Activity. This can be dones easily with an Intent: Intent intent = new Intent(getActivity(), ActivityB.class) intent.putExtra("KEY", <your required data to transfer>); getActivity().startActivityForResult(intent);

    Note that when you use putExtra() don't forget that you need to implement Parcelable in those objects (explained here)

    To get to know which item was clicked you can use the following pattern (pseudocode - I personally think it's really clean):

    FragmentA implements YourAdapter.callback {
       onItemClicked(<YourObject> item) {
           <starting new activity as described above>
       }
    }
    class YourAdapter extends RecyclerView.Adapter {
         Callback mCallback;
    
         YourAdapter(Context context, otherStuff) {
             mCallback = (Callback) context;
         }
    
         interface Callback {
             onItemClicked(<YourObject> item)
         }
    
         YourViewHolder implements View.OnClickListener {
              onClick(View v) {
                  mCallback.onItemClicked(<YourObject> item)
              }
         }
    }
    

    Once you are in your Activity, you can set the Fragment in onCreate() of your Activity. In the Activity retrieve the data with getIntent() in the onCreate before creating the Fragment. Then you can put your data in the Fragment with setArguments(<Bundle>). In the Fragment in the onCreateview() retrieve it with getArguments().

    I know this is kind of conmplicated. A better solution would be to just switch to an Activity and forget about the Fragment. This would remove one layer of complexity.

    If you directly go from Fragment to Fragment you can ignore the Activity part but the rest should stay the same.

    Hope this was the answer you were looking for!

    Edit: Note that mCallback = (Callback) context is null if the Activity is not implementing Callback