Skip to content

Commit 070b489

Browse files
author
Daniel Novak
committed
Pass identifier to viewmodel
1 parent 107c4fa commit 070b489

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

library/src/main/java/eu/inloop/viewmodel/AbstractViewModel.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,25 @@
88

99
public abstract class AbstractViewModel<T extends IView> {
1010

11+
@NonNull
12+
private String mUniqueIdentifier;
13+
1114
@Nullable
1215
private T mView;
1316

17+
void setUniqueIdentifier(String uniqueIdentifier) {
18+
mUniqueIdentifier = uniqueIdentifier;
19+
}
20+
21+
/**
22+
*
23+
* @return An app unique identifier for the current viewmodel instance (will be kept during orientation
24+
* change). This identifier will be reset in case the corresponding activity is killed.
25+
*/
26+
public String getUniqueIdentifier() {
27+
return mUniqueIdentifier;
28+
}
29+
1430
public void initWithView(@NonNull T view) {
1531
mView = view;
1632
}

library/src/main/java/eu/inloop/viewmodel/ViewModelProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public synchronized <T extends IView> ViewModelWrapper<T> getViewModel(@NonNull
4949

5050
try {
5151
instance = viewModelClass.newInstance();
52+
instance.setUniqueIdentifier(key);
5253
mViewModelCache.put(key, instance);
5354
return new ViewModelWrapper<>(instance, true);
5455
} catch (InstantiationException | IllegalAccessException e) {

0 commit comments

Comments
 (0)