forked from inloop/AndroidViewModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewModelStatePagerAdapter.java
More file actions
39 lines (33 loc) · 1.43 KB
/
ViewModelStatePagerAdapter.java
File metadata and controls
39 lines (33 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package eu.inloop.viewmodel.support;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
import android.view.ViewGroup;
import eu.inloop.viewmodel.BuildConfig;
import eu.inloop.viewmodel.IView;
/**
* This class extends {@link FragmentStatePagerAdapter}. It removes the ViewModel once the
* pager item is destroyed ({@link #destroyItem(ViewGroup, int, Object)}). The ViewModel state
* is stored and then restored once you return back to this pager item and {@link #instantiateItem(ViewGroup, int)}
* is called.
*/
public abstract class ViewModelStatePagerAdapter extends FragmentStatePagerAdapter {
public ViewModelStatePagerAdapter(@NonNull final FragmentManager fm) {
super(fm);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
super.destroyItem(container, position, object);
final Fragment fragment = (Fragment) object;
if (fragment instanceof IView) {
IView viewModelBaseFragment = (IView) fragment;
viewModelBaseFragment.removeViewModel();
} else {
if (BuildConfig.DEBUG) {
Log.w("model", "Fragment " + fragment + " in FragmentStatePagerAdapter " + this + " doesn't implent IView"); //NON-NLS
}
}
}
}