Java.lang.IllegalStateException: activity has been destroyed

Working with Robolectric, I am very new to Android. I made the first test class using Activity. It worked beautifully. Now I want to do a test for the 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 the Fragment class. He throws

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

Does anyone know how to fix this?

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

Thanks in advance!

+3
source share
4 answers

Fragments should be displayed from Activity. The stream must be:

  • select a new fragment object in the FragmentActivity class
  • get a fragment manager to add a newly selected fragment

In your case, you have no connection with real activities. You highlight FragmentActivity with the new FragmentActivity () and try to get a support manager. Although in this compilation there is no "real" action that can control your fragment. Snippets can be added to already shown actions, and here it is not.

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

+1
source

In my case, in particular, my problem was creating this activity. I used

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

And it must be

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

Hope this helps someone: D

+4
source
 @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 a working version. The following 3 lines are important (this is from the source of roboelectricity - DialogFragmentTest).

  shadowOf(activity).callOnCreate(null); shadowOf(activity).callOnStart(); shadowOf(activity).callOnResume(); 
+3
source

This happened to me when I used the Transaction.commitAllowingStateLoss () fragment; from a subfragment whose parent fragment is set to RetainInstance (true); I had an activity as a property that led to a leak of activity during rotation.

+1
source

Source: https://habr.com/ru/post/951107/


All Articles