in Education by
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)

1 Answer

0 votes
by
In its simplest form (and assuming Context is already part of your dependency graph), then in your module: @Provides static SharedPreferences prefs(Context context) { return PreferenceManager.getDefaultSharedPreferences(context); } And in your Presenter: class Presenter { private final SharedPreferences prefs; @Inject Presenter(SharedPreferences prefs) { this.prefs = prefs; } } There's more you can do with this however. For instance, you can scope the Presenter to the lifecycle of the View (i.e. an Activity or Fragment). Also, a "Presenter" in its purest form should know nothing of Android components, so you may want to abstract the SharedPreferences behind an API of your own. Edit: Here's an example of how you might abstract your data persistence so you Presenter can remain unaware of Android components: Create a suitable interface (this just persists and returns Strings, so modify it accordingly): interface Repository { String read(); void write(String value); } Create an instance of this interface that delegates to SharedPreferences: class SharedPrefsRespository implements Repository { private static final String KEY = "a_key"; private final SharedPreferences prefs; @Inject SharedPrefsRepository(SharedPreferences prefs) { this.prefs = prefs; } @Override String read() { return prefs.getString(KEY, ""); } @Override void write(String value) { prefs.edit() .putString(KEY, value == null ? "" : value) .apply(); } } Tell Dagger that this implementation of Repository is the one you want to use by adding the following to your module (ensure your module is abstract): @Binds abstract Repository repository(SharedPrefsRepository prefs); In your Presenter, inject Repository rather than SharedPreferences: class Presenter { private final Repository repo; private View view; @Inject Presenter(Repository repo) { this.repo = repo; } void attach(View view) { this.view = view; showUserName(); } //Example of how you might use this repo on something you want to persist //Imagine a user has entered their username and clicked save... void onSaveUserName(String username) { repo.write(username); } private void showUserName() { view.showUsername(repo.read()); } }

Related questions

0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    Can we implement transport layer security (TLS) in REST?...
asked Jun 23, 2021 in Technology by JackTerrance
0 votes
    I am currently trying to understand the architecture behind the word2vec neural net learning algorithm, for representing ... page 67. Select the correct answer from above options...
asked Jan 29, 2022 in Education by JackTerrance
0 votes
    I am new on socketCAN and wanted to use socketCAN lib in Linux but i don't have CAN hardware ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    How to create cascading filters without using context filter?...
asked Oct 30, 2020 in Technology by JackTerrance
0 votes
    I have two activities in my project one activity is MainActivity and another is Main2activity, In Main2activity ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have two activities in my project one activity is MainActivity and another is Main2activity, In Main2activity ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have two activities in my project one activity is MainActivity and another is Main2activity, In Main2activity ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I have two activities in my project one activity is MainActivity and another is Main2activity, In Main2activity ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I have two activities in my project one activity is MainActivity and another is Main2activity, In Main2activity ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 24, 2022 in Education by JackTerrance
0 votes
    I am trying to develop a message app within my android app. The message will be like whatsapp and ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which 2 protocols are used in the Transport layer of the TCP/IP model? (a) UDP and HTTP (b) TCP and UDP ( ... , Cyber Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 3, 2021 in Education by JackTerrance
0 votes
    In the context of computing, a byte is equal to _____ bits ? 1. 4 2. 16 3. 24 4. 8...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    A representation of an attribute that cannot be measured directly, and are subjective and dependent on the context of ... was derived. (1)Relative Metrics (2)Absolute Metrics...
asked May 15, 2021 in Technology by JackTerrance
0 votes
    Which of the following type of metrics do not involve subjective context but are material facts? (1)Absolute Metrics (2)Relative Metrics...
asked May 15, 2021 in Technology by JackTerrance
...