[Andoird] 3. MVVM에 사용 될 기초 Activity클래스 및 View인터페이스 만들기 - Steemit
참고
How to use abstraction with ViewBinding with base activity?
보일러 플레이트 코드를 줄이기 위한 BaseActivity 생성
BaseActivity
package com.example.testproject;
import android.os.Bundle;
import android.view.LayoutInflater;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;
public abstract class BaseActivity<VB extends ViewBinding> extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
VB binding = getViewBinding(getLayoutInflater());
setContentView(binding.getRoot());
initListener();
initSetting();
initData();
}
protected abstract VB getViewBinding(LayoutInflater inflater);
protected abstract void initListener();
void initData(){};
protected abstract void initSetting();
}
ChildActivity
package com.example.testproject;
import android.view.LayoutInflater;
import com.example.testproject.databinding.ActivityChildBinding;
public class ChildActivity extends BaseActivity<ActivityChildBinding> {
private ActivityChildBinding binding;
@Override
protected ActivityChildBinding getViewBinding(LayoutInflater inflater) {
binding = ActivityChildBinding.inflate(inflater);
initListener();
initSetting();
return binding;
}
@Override
protected void initListener() {
binding.childButton.setOnClickListener(v->{
binding.childTextview.setText("Good Bye");
});
}
@Override
protected void initSetting() {
binding.childTextview.setText("Bye");
}
}