How to use Shared Preferences in MVP without Dagger and not causing Presenter to be Context dependent?

Marian Paździoch

I'm trying to implement MVP without Dagger (for learning purposes). But I got to the problem - I use Repository patter to get raw data either from cache (Shared Preferences) or network:

Shared Prefs| 
            |<->Repository<->Model<->Presenter<->View
     Network|

But to put my hands on Shared Preferences I have to put somewhere line like

presenter = new Presenter(getApplicationContext());

I use onRetainCustomNonConfigurationInstance/getLastCustomNonConfigurationInstance pair to keep Presenter "retained".

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;
    }

    //...
}

So how to use Shared Preferences in MVP without Dagger and not causing Presenter to be Context dependent?

David Medenjak

Your Presenter should not be Context dependent in the first place. If your presenter needs SharedPreferences you should pass them in the constructor.
If your presenter needs a Repository, again, put that in the constructor. I highly suggest watching Google clean code talks since they do a really good job explaining why you should use a proper API.

This is proper dependency management, which will help you write clean, maintainable, and testable code. And whether you use dagger, some other DI tool, or supply the objects yourself is irrelevant.

public class MyActivity extends AppCompatActivity implements MvpView {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SharedPreferences preferences = // get your preferences
        ApiClient apiClient = // get your network handling object
        Repository repository = new Repository(apiClient, preferences);
        presenter = new Presenter(repository);
    }
}

This object creation can be simplified by using a factory pattern, or some DI framework like dagger, but as you can see above neither Repository nor your presenter depends on a Context. If you want to supply your actual SharedPreferences only their creation of them will depend on the context.

Your repository depends on some API client and SharedPreferences, your presenter depends on the Repository. Both classes can easily be tested by just supplying mocked objects to them.

Without any static code. Without any side effects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I use dagger 2 so that I can use an abstract representation of shared preferences?

From Dev

Trying to implement MVP using Dagger 2 - How can I get a reference to the Activity in the provided presenter

From Dev

Trying to implement MVP using Dagger 2 - How can I get a reference to the Activity in the provided presenter

From Dev

How to invoke Presenter Methods in Model file with Dagger2 and MVP pattern

From Dev

How can i inject object into presenter in android kotlin MVP mosby app with dagger

From Dev

How to connect Adapter with Presenter in MVP?

From Dev

Dagger with Android: How to inject context when using MVP?

From Dev

How to use primary key in Shared Preferences in android?

From Dev

How to use shared preferences in RecyclerView.Adapter?

From Dev

How to use primary key in Shared Preferences in android?

From Dev

How to use a class with a context argument in a static context without causing a memory leak?

From Dev

Use Shared Preferences in xamarin

From Dev

MVP: how to write to log from Presenter

From Dev

How to unit-test a presenter in a MVP

From Dev

How to pass EventArgs from View to Presenter in MVP?

From Dev

Multiple shared preferences causing default values

From Dev

Does the presenter having knowledge of the Activity / Context a bad idea in the MVP pattern?

From Dev

how to retrive shared preferences from another class without activity ANDROID

From Java

How to use shared preferences to keep user logged in flutter?

From Dev

How do I use shared preferences in a fragment on Android?

From Dev

how to use shared preferences to save array list items?

From Dev

how to use shared preferences to save array list items?

From Dev

How to use shared preferences to save data that get from database?

From Dev

Correct use of Model in Presenter with MVP-VM Design Pattern

From Dev

Why do we use Base View and Base Presenter for MVP pattern?

From Dev

Allowing View To Use Presenter's Model Property For Databinding in MVP

From Dev

How to transfer data between presenter and data service in MVP

From Dev

Android MVP How to get data back from model to presenter

From Dev

How to Secure Android Shared Preferences?

Related Related

  1. 1

    How can I use dagger 2 so that I can use an abstract representation of shared preferences?

  2. 2

    Trying to implement MVP using Dagger 2 - How can I get a reference to the Activity in the provided presenter

  3. 3

    Trying to implement MVP using Dagger 2 - How can I get a reference to the Activity in the provided presenter

  4. 4

    How to invoke Presenter Methods in Model file with Dagger2 and MVP pattern

  5. 5

    How can i inject object into presenter in android kotlin MVP mosby app with dagger

  6. 6

    How to connect Adapter with Presenter in MVP?

  7. 7

    Dagger with Android: How to inject context when using MVP?

  8. 8

    How to use primary key in Shared Preferences in android?

  9. 9

    How to use shared preferences in RecyclerView.Adapter?

  10. 10

    How to use primary key in Shared Preferences in android?

  11. 11

    How to use a class with a context argument in a static context without causing a memory leak?

  12. 12

    Use Shared Preferences in xamarin

  13. 13

    MVP: how to write to log from Presenter

  14. 14

    How to unit-test a presenter in a MVP

  15. 15

    How to pass EventArgs from View to Presenter in MVP?

  16. 16

    Multiple shared preferences causing default values

  17. 17

    Does the presenter having knowledge of the Activity / Context a bad idea in the MVP pattern?

  18. 18

    how to retrive shared preferences from another class without activity ANDROID

  19. 19

    How to use shared preferences to keep user logged in flutter?

  20. 20

    How do I use shared preferences in a fragment on Android?

  21. 21

    how to use shared preferences to save array list items?

  22. 22

    how to use shared preferences to save array list items?

  23. 23

    How to use shared preferences to save data that get from database?

  24. 24

    Correct use of Model in Presenter with MVP-VM Design Pattern

  25. 25

    Why do we use Base View and Base Presenter for MVP pattern?

  26. 26

    Allowing View To Use Presenter's Model Property For Databinding in MVP

  27. 27

    How to transfer data between presenter and data service in MVP

  28. 28

    Android MVP How to get data back from model to presenter

  29. 29

    How to Secure Android Shared Preferences?

HotTag

Archive