java.lang.IllegalStateException: Activity has been destroyed

12,887

Solution 1

The fragments are supposed to be displayed from an Activity. The flow should be:

  • allocate a new fragment object in a FragmentActivity class
  • get the fragment manager to add the newly allocated fragment

In your case you do not have a connection to a real activity. You allocate a FragmentActivity with new FragmentActivity() and try to get the support manager. While this compiles there is no "real" activity able to manage your fragment. Fragments can be added on activities already displayed and here it's not the case.

I recommend reading this page as it explains these things very well: http://developer.android.com/guide/components/fragments.html

Solution 2

In my case, specifically, my problem was when creating the activity. I was using

 activity = Robolectric.buildActivity(MyActivity.class).get();

And it should be

 activity = Robolectric.buildActivity(MyActivity.class).create().get();

Hope it helps someone :D

Solution 3

@RunWith(RobolectricTestRunner.class)
public class LoginFragmentTest {
    private LoginFragment fragment;

    @Before
    public void setup() {
        fragment = new LoginFragment();
        startFragment();
        assertThat(fragment, notNullValue());
        assertThat(fragment.getActivity(), notNullValue());
    }

    private void startFragment() {
        FragmentActivity activity = new FragmentActivity();
        shadowOf(activity).callOnCreate(null);
        shadowOf(activity).callOnStart();
        shadowOf(activity).callOnResume();

        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(fragment, null);
        fragmentTransaction.commit();
    }

    @Test
    public void login() {
        EditText idEditText = (EditText) fragment.getView().findViewById(R.id.main_id);
        assertThat(idEditText, notNullValue());
    }
}

This is working version. Following 3 lines are important(it's from robolectric source - DialogFragmentTest).

        shadowOf(activity).callOnCreate(null);
        shadowOf(activity).callOnStart();
        shadowOf(activity).callOnResume();

Solution 4

That happened for me when I used fragmentTransaction.commitAllowingStateLoss(); from sub Fragment whose parent fragment had setRetainInstance(true); I had activity as property what lead to leaking activity on rotation.

Share:
12,887
Myeonseok Baek
Author by

Myeonseok Baek

Updated on June 08, 2022

Comments

  • Myeonseok Baek
    Myeonseok Baek about 2 years

    Working with Robolectric , I'm very new to android. I made a first test class using Activity. It worked nicely. Now I want make a test for fragment.

    @RunWith(RobolectricTestRunner.class)
    public class LoginFragmentTest {
        private LoginFragment fragment;
    
        @Before
        public void setup() {
            fragment = new LoginFragment();
            startFragment(fragment);
            assertThat(fragment, notNullValue());
            assertThat(fragment.getActivity(), notNullValue());
        }
    
        private void startFragment(LoginFragment fragment) {
            FragmentManager fragmentManager = new FragmentActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(fragment, null);
            fragmentTransaction.commit();
        }
    
        @Test
        public void login() {
            EditText idEditText = (EditText)fragment.getActivity().findViewById(R.id.main_id);
            assertThat(idEditText, notNullValue());
        }
    }
    

    This is my first test class for Fragment class. It throws

    "java.lang.IllegalStateException: Activity has been destroyed" in startFragment#fragmentTransaction.commit().
    

    Anyone knows how to fix this ?

    You can find whole source from https://github.com/msbaek/frame-test

    Thanks in advance !!

    • njzk2
      njzk2 over 11 years
      hum, don't destroy your activity before commiting a fragment transaction ?
  • Myeonseok Baek
    Myeonseok Baek over 11 years
    Thanks !! I succeeded in connect activity to fragment.
  • MaciejGórski
    MaciejGórski about 11 years
    What does "real" activity mean in context of Robolectric?
  • muneikh
    muneikh about 9 years
    @백명석 What did you do to connect the fragment to the activity? I am experiencing the similar problem.