Processing a dagger component when changing orientation

Assuming what was said here , it is the developer’s responsibility to preserve the component instance to implement its own visibility logic (since the scope of the method will return the same instance for this component).

What is a clean way to maintain a link to this component through the activity life cycle?

Example. You are implementing the MVP template, so you need a presenter in your work. This presenter can perform a network operation to load items. When the device rotates, your activity is destroyed and recreated, but you want the network to continue, and just return the presentation before rotating.

Finding a component that provides Presenter with a PerActivity custom scope is a solution, so you need to save the component instance through this rotation in order to get the same Presenter instance as the first start of the Activity.

How can we handle this? I was thinking of some kind of component cache (like HashMap?), Which could be provided by the application component living inside the Application class.

+6
source share
2 answers

You can see the implementation of the ribot / android-boilerplate application . The solution they chose should have a static Map<Long, ConfigPersistentComponent> inside BaseActivity from which all actions propagate.

 public class BaseActivity extends AppCompatActivity { private static final AtomicLong NEXT_ID = new AtomicLong(0); private static final Map<Long, ConfigPersistentComponent> sComponentsMap = new HashMap<>(); private ActivityComponent mActivityComponent; private long mActivityId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create the ActivityComponent and reuses cached ConfigPersistentComponent if this is // being called after a configuration change. mActivityId = savedInstanceState != null ? savedInstanceState.getLong(KEY_ACTIVITY_ID) : NEXT_ID.getAndIncrement(); ConfigPersistentComponent configPersistentComponent; if (!sComponentsMap.containsKey(mActivityId)) { // Creating new component configPersistentComponent = DaggerConfigPersistentComponent.builder() .applicationComponent(BoilerplateApplication.get(this).getComponent()) .build(); sComponentsMap.put(mActivityId, configPersistentComponent); } else { // Reusing component configPersistentComponent = sComponentsMap.get(mActivityId); } mActivityComponent = configPersistentComponent.activityComponent(new ActivityModule(this)); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putLong(KEY_ACTIVITY_ID, mActivityId); } @Override protected void onDestroy() { if (!isChangingConfigurations()) { // Activity is finishing, removing the component sComponentsMap.remove(mActivityId); } super.onDestroy(); } ... } 
0
source

The network can work with the application context. Here is how I could Applicationcomponent with appscope Now I would create this at the application level ApplicationComponent Shouod uses context as an external dependency

Next is an activity component expanding modulo an activity with an activity coefficient. Depending on the component application

In each of my actions I would create an activityComponet, giving it an applicationcomponet. This application component can be accessed through. Activity.getapplication (). Getapplicationcomponent ()

Here, make sure that your network method for providing network applications has an application. If so, then u should get the same network even in the rorate application.

Look for a sample GitHubapplication, place the link in the next edit.

He should also take a look at the zhitat (out of context for this question)

0
source

All Articles