android - "Activity has been destroyed" error on Fragment testing with Robolectric -


i'm trying make fragment testing on android robolectric , know there's been other posts problems fragmentactivity when hasn't been stored in variable.

however, trying make this post work , can see in case fragmentactivity object reference kept there shouldn't problem that. here base class:

import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity;  import org.junit.after; import org.robolectric.robolectric; import org.robolectric.util.activitycontroller;  public class fragmenttestcase<t extends fragment> {  private static final string fragment_tag = "fragment";  private activitycontroller controller; private fragmentactivity activity; private t fragment;  public void startfragment(t fragment) {     this.fragment = fragment;     controller = robolectric.buildactivity(fragmentactivity.class);     activity = (fragmentactivity) controller.create().start().visible().get();     activity.getsupportfragmentmanager()             .begintransaction()             .add(fragment, fragment_tag).commit(); }  @after public void destroyfragment() {     if (fragment != null) {         activity.getsupportfragmentmanager()                 .begintransaction()                 .remove(fragment)                 .commit();          fragment = null;     } }  public void pauseandresumefragment() {     controller.pause().resume(); }  public t recreatefragment() {     activity.recreate();     fragment = (t) activity.getsupportfragmentmanager()             .findfragmentbytag(fragment_tag);      return fragment;  } } 

and here implementation of test:

import org.junit.before; import org.junit.test; import org.junit.runner.runwith; import org.robolectric.robolectricgradletestrunner; import org.robolectric.annotation.config;  import static org.assertj.core.api.assertions.*;  @runwith(robolectricgradletestrunner.class) @config(constants=buildconfig.class, sdk=21) public class mainactivityfragmenttest extends fragmenttestcase<mainactivityfragment> {  private mainactivityfragment fragment;  @before public void setup() throws exception {     fragment = new mainactivityfragment(); }  @test public void createsanddestroysfragment() throws exception {     startfragment(fragment);     assertthat(fragment).isnotnull(); }  @test public void pausesandresumesfragment() throws exception {     startfragment(fragment);     pauseandresumefragment();     assertthat(fragment).isnotnull(); }  @test public void recreatesfragment() throws exception {     startfragment(fragment);     fragment = recreatefragment();     assertthat(fragment).isnotnull();   }  } 

once run test error get:

java.lang.illegalstateexception: activity has been destroyed @ android.support.v4.app.fragmentmanagerimpl.enqueueaction(fragmentmanager.java:1399) @ android.support.v4.app.backstackrecord.commitinternal(backstackrecord.java:637) @ android.support.v4.app.backstackrecord.commit(backstackrecord.java:616) @ com.feyserflabs.musync.fragmenttestcase.destroyfragment(fragmenttestcase.java:36) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:50) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:47) @ org.junit.internal.runners.statements.runafters.evaluate(runafters.java:33) @ org.robolectric.robolectrictestrunner$2.evaluate(robolectrictestrunner.java:245) @ org.robolectric.robolectrictestrunner.runchild(robolectrictestrunner.java:185) @ org.robolectric.robolectrictestrunner.runchild(robolectrictestrunner.java:54) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.robolectric.robolectrictestrunner$1.evaluate(robolectrictestrunner.java:149) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.junit.runner.junitcore.run(junitcore.java:137) @ com.intellij.rt.execution.application.appmain.main(appmain.java:140) 

it seems @ point activity destroyed before entering destroyfragment method, can't find o why. appreciated.

when call activity.recreate() destroy current activity , reference keep becomes invalid. try use fragmentmanager of destroyed activity , crashes.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -