-1

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 ?

rsanath
  • 1,154
  • 2
  • 15
  • 24
  • The thing that makes this more complex is that I have to write all the code within the adapters of recycler view. Any other work around for this. Without filling all my code in adapter ? – rsanath Jun 18 '17 at 02:43

2 Answers2

0


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));
Zhyano
  • 399
  • 1
  • 3
  • 13
0

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

Rene Ferrari
  • 4,096
  • 3
  • 22
  • 28