Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How to implement sharedpreferences with dagger and MVP, most of the tutorials are writing the sharedpreferences in the Activity or providing the context to presenter.
what i need is to see example:
what should i write into my presenter to get the sharedPreference without using the Context.
how the module will look like.
Do i need to call the Component from Application or with every activity to provide the context.
===Update===
As PPartisan mentioned in his answer that i should't include the sharedPreference in the Presenter.
So how can i abstract the SharedPreferences behind an API
===Update 2===
Thanks for your answer PPartisan it really worked very good, could you please check my code if its perfectly written, or need any adjustments.
MainActivity
@Inject
MainPresenter presenter;
AppComponent component;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
component = DaggerAppComponent
.builder()
.contextModule(new ContextModule(this))
.build();
component.inject(this);
}
AppModule
@Module
public abstract class AppModule {
@Binds
abstract Repository bindRepository(SharedPrefsRepository prefs);
@Binds
abstract MainPresenterListener listener(MainPresenterListener mListener);
@Provides
static SharedPreferences prefs(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
}
ContextModule
@Module
public class ContextModule {
private Context context;
public ContextModule(Context context) {
this.context = context;
}
@Provides
Context getContext() {
return context;
}
}
AppComponent
@Component (modules = {AppModule.class, ContextModule.class})
public interface AppComponent {
void inject(MainActivity activity);
}
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)