Search code examples
androidfragmentfragmenttransaction

Create New Fragment from RecyclerViewAdapter class


I am trying to create a new fragment instance upon the user clicking an item in my recycler view. It is crashing on the line: "fragmentTransactiion.commit()" throwing a JavaIllegalStateException saying that the Activity has been destroyed. Here is the code in my RecyclerViewAdapter class that launches the new fragment:

    @Override
    public void onClick(View v) {
        String poster_id = event_picture.getProfileId();
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference()
                .child("events").child("userid").child(poster_id);
        if (databaseReference != null) databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Event event = dataSnapshot.getValue(Event.class);
                String clickedEventName = event_name.getText().toString();
                String clickedEventLocation = event_location.getText().toString();
                String eventName = event.getTitle();
                String eventAddress = event.getAddress();

                if (clickedEventLocation.equals(eventAddress) && clickedEventName.equals(eventName)){
                    displayEvent(event);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

public static void displayEvent(Event e){
    Bundle bundle = getBundle(e);
    DisplayEvent displayEvent = new DisplayEvent();
    displayEvent.setArguments(bundle);
    AppCompatActivity context = new AppCompatActivity();
    FragmentManager fragmentManager = context.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(android.R.anim.slide_in_left,
            android.R.anim.slide_out_right);
    fragmentTransaction.replace(R.id.fragment_display, displayEvent, "display_event");
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

I'm not exactly sure what I'm doing wrong here. Any help would be appreciated!!


Solution

  • You are trying to access the context of a non-existent/created Activity on the following lines.

    AppCompatActivity context = new AppCompatActivity();
    FragmentManager fragmentManager = context.getSupportFragmentManager();
    

    You should change it to something like

    FragmentManager fragmentManager = activity.getSupportFragmentManager();
    

    You can achieve this by holding a reference to the activity that the RecyclerView belongs to, where it's being created. The activity in the above code block should be the one that created it and not a new Activity instance.

    A better approach will be to have your own custom interface as callback that notifies its listener(activity).Then, the activity should do the Fragment or Activity transitions instead.