我正在尝试在没有Dagger的情况下实现MVP(出于学习目的)。但是我遇到了一个问题-我使用Repository模式从缓存(Shared Preferences)或网络获取原始数据:
Shared Prefs|
|<->Repository<->Model<->Presenter<->View
Network|
但是要使用Shared Preferences,我必须在某个地方放置类似于以下的代码行
presenter = new Presenter(getApplicationContext());
我使用onRetainCustomNonConfigurationInstance
/getLastCustomNonConfigurationInstance
这一对功能来保持Presenter“保留”。
public class MyActivity extends AppCompatActivity implements MvpView {
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
presenter = (MvpPresenter) getLastCustomNonConfigurationInstance();
if(null == presenter){
presenter = new Presenter(getApplicationContext());
}
presenter.attachView(this);
}
@Override
public Object onRetainCustomNonConfigurationInstance() {
return presenter;
}
//...
}
不使用Dagger,且不使Presenter依赖于Context,如何在MVP中使用Shared Preferences?
presenter
依赖于repository
,而repository
又依赖于shared pref
...... 这是否意味着presenter
间接地依赖于shared pref
?还是我漏掉了什么核心内容? - eRaisedToX