I have a Mortar application, with MortarActivityScope as the first child under the root area. In MortarActivityScope there is ActivityScope, which @Provides activity for the classes introduced:
@Module(addsTo = ApplicationModule.class, injects = {Foo.class, SomePresenter.class, AnotherPresenter.class}) public class ActivityModule { private final Activity activity; public ActivityModule(Activity activity) { this.activity = activity; } @Provides Activity provideActivity() { return activity; } } public class Foo { private final Activity activity; @Inject(Activity activity) { this.activity = activity; } public void doSomethingWithActivity() {
This is normal until a change in orientation occurs. In a typical mortar project, the scope does not change when the orientation changes. This seems to allow @Singleton resellers, screens, etc. Save orientation changes. You can see this in the onDestroy () method in the main project of the main project:
@Override protected void onDestroy() { super.onDestroy(); actionBarOwner.dropView(this); // activityScope may be null in case isWrongInstance() returned true in onCreate() if (isFinishing() && activityScope != null) { MortarScope parentScope = Mortar.getScope(getApplication()); parentScope.destroyChild(activityScope); activityScope = null; } } }
However, the execution of this method means that the old ObjectGraph is saved in a change of orientation. I noticed that Mortar.requireActivityScope not replacing a module from the old scope with the new module provided by the new Blueprint. Instead, the object graph maintains a link to the previous module, including the destroyed action.
public class MyActivity extends Activity implements Blueprint { @Inject foo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MortarScope parentScope = Mortar.getScope(getApplication()); activityScope = Mortar.requireActivityScope(parentScope, this); Mortar.inject(this, this); foo.doSomethingWithActivity();
The activity of the Mortar pattern seems to have circumvented this, not including the @Provides Activity method in the main module. But shouldn't MortarActivityScope introduce activity? What is the preferred way to do this without losing all of your singleton objects ( Presenter objects, etc.) when changing orientation?