0

Hi I have A RecycleView Adapter and A button. I want that button to start a Fragment. I can start an activity but not a fragment. I have tried this Onclick method for my Button

@Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putParcelable("event", events.get(getLayoutPosition()));
            Fragment fragment = new EditEventDetailFragment();
            fragment.setArguments(bundle);
            fragment.getFragmentManager().beginTransaction().replace(R.id.contentMainDrawer,fragment).commit();
        }

But have error invoke null object (contentMainDrawer) is my Main activity content_layout.

Any help is much appreciate. The Fragment host recycle view is call from Mainactivity

Lê Khánh Vinh
  • 2,591
  • 5
  • 31
  • 77
  • Uhm, you are not supposed to use FragmentManager from the Fragment that you have just created. Replace this line fragment.getFragmentManager() with just getFragmentManager() (or getSupportFragmentManager() depending on what kind of Fragment you are using) – scana Dec 30 '15 at 10:00
  • thanks for yrs help but the code is inside the RecycleView adapter (not another activity or fragment) – Lê Khánh Vinh Dec 30 '15 at 10:11
  • Well, if you really need to do this this way, then you can do this: ((Activity) v.getContext).getFragmentManager(). But this is not really a good approach. What you could is to move onClick's handling to the parent class. Take a look at this post: http://stackoverflow.com/a/24933117/1139527 – scana Dec 30 '15 at 10:16
  • can not seem use the ((Activity) v.getContext).getFragmentManager(). Move OnClick handling mean OnClick will be handled in the class that use the adapter? but it's hard to get the item in the click position? – Lê Khánh Vinh Dec 30 '15 at 10:24
  • I've written this really quick. I hope that this will give you the basic idea on how to do achieve it: https://gist.github.com/scana/8ced6d423c91142c36ea – scana Dec 30 '15 at 10:31
  • thanks a lot. so where do we put the intereface public interface MyItemClickListener and the public void onClick(View v) ? – Lê Khánh Vinh Dec 30 '15 at 10:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99297/discussion-between-scana-and-le-khanh-vinh). – scana Dec 30 '15 at 10:43
  • Its better to make interface ! – Piyush Dec 30 '15 at 10:47
  • Many thanks for @scana. Spend lot of time help me create an interface and debugging. will post the answer for other who need – Lê Khánh Vinh Dec 30 '15 at 11:27

2 Answers2

0

Use below code for replace fragment

 Fragment fragment = new EditEventDetailFragment();
 FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 fragmentTransaction.replace(R.id.contentMainDrawer, fragment, "tag").commit();

if you are using this code inside adapter than replace getActivity() to ((Activity) context).

Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
  • thanks for your respond but the code is inside recycleView adapter can not call getActivity() or i dont know how to call getActivity() – Lê Khánh Vinh Dec 30 '15 at 10:10
  • I tried few time but this line is not working FragmentManager fragmentManager = ((Activity) context).getSupportFragmentManager(); cannot call the getSupportFragmentManager – Lê Khánh Vinh Dec 30 '15 at 11:15
  • replace getSupportFragmentManager to getChildFragmentManager() – Mohit Suthar Dec 30 '15 at 11:17
  • I Got this line FragmentManager fragmentManager = ((Activity) context).getFragmentManager(); conflict because my fragment use support v4 (I still dont know whether to use app.fragment or support v4.fragment – Lê Khánh Vinh Dec 30 '15 at 11:40
  • and also cannot change getFragmentManager to getChildFragmentManager – Lê Khánh Vinh Dec 30 '15 at 11:40
0

Hi I have found 2 answer that help whoever needed. The answer is to use the Activity that host the calling fragment. Many thanks @Mohit Suthar

Bundle bundle = new Bundle();
            bundle.putParcelable("event", events.get(getLayoutPosition()));
            Fragment fragment = new EditEventDetailFragment();
            fragment.setArguments(bundle);

            FragmentManager fragmentManager = ((MainActivity) context).getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.contentMainDrawer, fragment, "tag").commit();
Lê Khánh Vinh
  • 2,591
  • 5
  • 31
  • 77