package com.example.muhanxi.rxjavademo.base;
import android.app.Activity;
import android.os.Bundle;
public abstract class BaseMvpActivity<V,T extends BasePresenter<V>> extends Activity {
public abstract T initPresenter();
public T presnter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presnter = initPresenter();
}
@Override
protected void onResume() {
super.onResume();
presnter.attach((V) this);
}
@Override
protected void onDestroy() {
super.onDestroy();
presnter.detach();
}
}
BaseMvpFragment
package com.example.muhanxi.rxjavademo.base;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.muhanxi.rxjavademo.R;
/**
* A simple {@link Fragment} subclass.
*/
public abstract class BaseMvpFragment<V,T extends BasePresenter<V>> extends Fragment {
public abstract T initPresenter();
public T presenter ;
public Context context ;
public BaseMvpFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
presenter = initPresenter() ;
context = getActivity() ;
return null;
}
@Override
public void onResume() {
super.onResume();
presenter.attach((V) this);
}
@Override
public void onDetach() {
super.onDetach();
presenter.detach();
}
}
BasePresenter
package com.example.muhanxi.rxjavademo.base;
/**
* Created by muhanxi on 17/12/5.
*/
public class BasePresenter<V> {
public V view ;
public void attach(V v){
this.view = v;
}
public void detach(){
this.view = null;
}
}