Created
March 6, 2018 05:03
-
-
Save gracefulife/1d831c056bfd2ab9cbe65d1c0f1bed73 to your computer and use it in GitHub Desktop.
MVVM base fragment format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package XXXXX; | |
import android.arch.lifecycle.ViewModel; | |
import android.os.Bundle; | |
import android.support.annotation.LayoutRes; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import butterknife.ButterKnife; | |
public abstract class BaseFragment<VM extends ViewModel> extends Fragment { | |
protected VM viewModel; | |
/** | |
* @return container layout id :) R.layout.fragment_XXX | |
*/ | |
protected abstract @LayoutRes int getLayoutResId(); | |
/** | |
* process | |
* 1. butter-knife bind | |
* 2. provided view-model | |
* 3. init components | |
* 4. initViews | |
* 5. subscribeDataStream | |
* | |
* @return rootView | |
*/ | |
@Nullable @Override | |
public View onCreateView(LayoutInflater inflater, | |
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(getLayoutResId(), container, false); | |
ButterKnife.bind(this, view); | |
this.viewModel = performViewModelInjection(); | |
initComponents(view); | |
initViews(view); | |
subscribeDataStream(view); | |
return view; | |
} | |
protected abstract VM performViewModelInjection(); | |
/** | |
* object initialize :) retrofit adapter | |
* | |
* @param rootView fragment container | |
*/ | |
protected abstract void initComponents(View rootView); | |
/** | |
* default data/state bind to view | |
* | |
* @param rootView fragment container | |
*/ | |
protected abstract void initViews(View rootView); | |
/** | |
* subscribe live-data | |
* | |
* @param rootView fragment container | |
*/ | |
protected abstract void subscribeDataStream(View rootView); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
직접 라이프 사이클을 관리하는 경우 스페셜라이즈된 클래스에서 직접 implements LifecycleOwner 를 해서, Provide 를 해주면 되고,
activity 것을 가져와서 사용해도 된다.