使用Dagger2+MVP架构时,如何恢复多步骤表单应用的状态?
Hey there! Let's walk through how to handle app state restoration in your step-by-step form setup—since you're using MVP, Dagger 2 with @ActivityScope, and a shared data object across Fragments, we can focus on leveraging your existing architecture instead of reinventing the wheel.
1. 核心:持久化与恢复共享表单数据对象
Your @ActivityScope shared data object is the single source of truth for all form data, so this is where we'll anchor our state handling:
- First, make sure this data class implements
Parcelable(preferred for speed) orSerializableso it can be saved to aBundle. For example:@ActivityScope public class FormData implements Parcelable { // Your form fields: name, email, address, etc. private String firstName; private String email; // Parcelable implementation: constructor from Parcel, writeToParcel, describeContents // Plus a method to copy data from another FormData instance public void copyFrom(FormData source) { this.firstName = source.firstName; this.email = source.email; // Copy all other fields } } - In your host Activity, even though you said it has no direct association with the FormData, you'll need to inject it temporarily to handle state save/restore (this doesn't break your architecture—it's just lifecycle handling). In
onSaveInstanceState:@Inject FormData formData; @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable("STEP_FORM_DATA", formData); } - When the Activity is recreated (e.g., after rotation or system kill), restore the data to the new
@ActivityScopeinstance inonCreate:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize Dagger component and inject dependencies DaggerYourActivityComponent.create().inject(this); if (savedInstanceState != null) { FormData savedFormData = savedInstanceState.getParcelable("STEP_FORM_DATA"); if (savedFormData != null) { formData.copyFrom(savedFormData); } } }
2. Fragment UI状态同步
Since each Fragment's Presenter depends on the shared FormData, restoring the data object will automatically give Presenters access to the saved form values. You just need to make sure Fragments refresh their UI when they're resumed or recreated:
- In each Fragment's
onViewCreatedoronResume, have the Presenter fetch the relevant step data fromFormDataand update the UI. For example:@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); presenter.loadStepData(); // Presenter pulls data from FormData and calls view.updateFields() } - For temporary UI state (like scroll position or input focus), you can still use Fragment's own
onSaveInstanceStateto save those small bits, but the core form data stays in the shared object.
3. Dagger 2 Scope Considerations
Remember that @ActivityScope creates a new instance of FormData every time the Activity is recreated. That's why we need to explicitly copy the saved data into the new instance—Dagger won't do this automatically.
- If you want to avoid injecting
FormDatadirectly into the Activity, you could create a helper class (also@ActivityScope) that handles saving/restoring the data, and inject that helper into the Activity instead. This keeps the Activity decoupled from the raw form data if that's important to you.
4. Edge Cases to Cover
- System-initiated kills: Android might kill your app when it's in the background, but the
Bundlepassed toonCreatewill still contain your savedFormData—so your restore logic will handle this seamlessly. - Async operations: If any Presenters are running async tasks (like validating input), make sure they cancel old tasks when the Fragment is destroyed, and re-initiate them with the restored data when the Fragment is recreated.
- Nested objects: If
FormDatacontains custom objects, make sure those also implementParcelable/Serializableto avoid serialization errors.
That's the gist! By focusing on persisting your single source of truth (the @ActivityScope FormData) and syncing Fragments to it, you'll have solid state restoration that fits cleanly into your MVP + Dagger setup.
内容的提问来源于stack exchange,提问作者Eselfar




