How to @ Skip activity for MortarActivityScope without leaking orientation change activity?

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() { // do stuff with activity: findViewById(), getWindow(), mess with action bar etc. } } 

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(); //fails, because activity injected by object graph is destroyed } @Override public String getMortarScopeName() { return getClass().getName(); } @Override public Object getDaggerModule() { return new ActivityModule(this); } } 

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?

+5
source share
1 answer

Do not let anyone introduce an action that cannot be made secure. Instead, enter a presenter bound to an Activity.

How to handle onActivityResult () with Mortar includes an example action that owns the presenter. Other parts of your application, including other presenters, can then enter this text and ask him to do everything they need, which requires working with it.

And there is no need to link all activities related to specific actions to one event leader. In our activity there are several speakers who offer their services to the rest of the application.

+5
source

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


All Articles