Search code examples
androidandroid-fragmentsfragmenttransaction

Call a Fragmant Transaction within a RecyclerView using OnClick


I want to replace a Fragment with another one using onClick(). My problem is, that I get an error like this:

java.lang.IllegalStateException: Activity has been destroyed

My MainActivity:

private static MainActivity instance = new MainActivity();

public static MainActivity getInstance() {
    return instance;
}

public void displayView() {
    fragment = new PlaylistFragment();
    FragmentManager fm = getSupportFragmentManager();
      if(fm.getBackStackEntryCount() != 0) {
          fm.popBackStack(fm.getBackStackEntryAt(0).getName(), 
          FragmentManager.POP_BACK_STACK_INCLUSIVE);
      }
    fm.beginTransaction().replace(R.id.frame_container, fragment,  
    fragment.toString()).commit();
 }

Layout:

Activity_main.xml:
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"        
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>

ListAdapter for my Fragment: Within the onBindViewHolder method:

View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder)  
            view.getTag();

            int position = holder.getLayoutPosition();
            System.out.println("CLICK");
            Artist artist = artists.get(position);

            MainActivity instance = MainActivity.getInstance();
            instance.displayView();

        }
    };

So the onClickListener works fine but I get the IllegalStateException. I debugged it and in the FragmentManager Class, it throws the error since "mHost" seems to be null:

 public void enqueueAction(Runnable action, boolean allowStateLoss) {
        if (!allowStateLoss) {
            checkStateLoss();
        }
        synchronized (this) {
            if (mDestroyed || mHost == null) {
                throw new IllegalStateException("Activity has been destroyed");
            }

Thanks in advance.


Solution

  • Why do you use private static MainActivity instance = new MainActivity();. Instance should be in onCreate() method like this:

    private static MainActivity instance = this;
    

    It creates new activity in your onClick method :

    MainActivity instance = MainActivity.getInstance();
                instance.displayView();