728x90
본 글은 AAC의 ViewModel 용 관련 글입니다.
1. Dependaceise 선언 관련
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
dependencies {
def lifecycle_version = "2.0.0"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // For Kotlin use lifecycle-viewmodel-ktx
// alternatively - just LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
// AndroidX libraries use this lightweight import for Lifecycle
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // For Kotlin use lifecycle-reactivestreams-ktx
// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
}
|
cs |
2. 추가시 유의사항
implementation'android.arch.lifecycle:ViewModel:1.1.1' //이 아니라
implementation'android.arch.lifecycle:extensions:1.1.1' //이쪽으로 추가할 것
==> ViewModel로만 연결하면, ViewModelProviders가 없음 (3번참고)
3. ViewModel 초기화 변경사항
androidx.lifecycle:lifecycle-*:2.2.0-alpha03 부터는 ViewModelProviders.of()로 초기화하던 방식이 deprecated 되었다. (아래 링크 참조)
- ViewModelProviders.of() has been deprecated. You can pass a Fragment or FragmentActivity to the new ViewModelProvider(ViewModelStoreOwner) constructor to achieve the same functionality. (aosp/1009889)
먼저, 다음과 같은 커스텀 ViewModel 클래스가 있다고 할 때, (JAVA 방식)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class TestViewModel extends ViewModel {
private MutableLiveData<String> test = new MutableLiveData<>();
public MutableLiveData<String> getTest() {
return test;
}
public void setTest(MutableLiveData<String> test) {
this.test = test;
}
}
|
cs |
API 변경전 (JAVA 방식)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class MainActivity extends AppCompatActivity {
private TestViewModel viewModel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//Activity에서 호출시 (ViewModelProvider.AndroidViewModelFactory 사용)
viewModel = ViewModelProviders.of(this).get(TestViewModel.class);
}
}
|
cs |
API 변경후 (JAVA 방식)
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
|
public class MainActivity extends AppCompatActivity implements ViewModelStoreOwner{
private ViewModelProvider.AndroidViewModelFactory viewModelFactory;
private ViewModelStore viewModelStore = new ViewModelStore();
private TestViewModel viewModel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if(viewModelFactory == null){
viewModelFactory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
}
viewModel = new ViewModelProvider(this, viewModelFactory).get(TestViewModel.class);
}
@Override
protected void onDestroy() {
super.onDestroy();
viewModelStore.clear();
}
@NonNull
@Override
public ViewModelStore getViewModelStore() {
return viewModelStore;
}
}
|
cs |
728x90
'Dev > Android' 카테고리의 다른 글
[Android] SurfaceView - surfaceCreated, surfaceDestoryed (0) | 2019.09.26 |
---|---|
[해석] 모듈식 안드로이드 앱 아키텍쳐를 구축하라 (1) (Google I/O'19) (0) | 2019.09.09 |
[요약] Android Jetpack: CameraX 라이브러리 알아보기 (Google I/O'19) (0) | 2019.09.04 |
[Android] DataBinding 설정 요약 (0) | 2019.09.03 |
[Android] 데이터 바인딩 개념 및 라이브러리 비교(Android Data Binding Library, Kotlin Android Extensions) (0) | 2019.09.02 |