4

I am new to android MVP pattern and working on my project i have some basic problem related to Android Context in the presenter. Although there are many answers related to this but i didn't get a perfect one which can solve my problem.

I have following queries:

  1. how to access shared preferences inside presenter.
  2. how to access other system services inside presenter.
  3. if i am working on SQLite Databases then during any transaction in my database which is done by call from presenter to my SQLite Helper class need context to access database.

If i will pass my activity context in presenter then it will a problem during unit testing, also it is a violation according to MVP Format. I need a perfect solution so that my code quality is not degraded.

Note: I dont want to use dagger tool so the answer should be dagger independent

saksham
  • 3,173
  • 1
  • 21
  • 23

1 Answers1

4

In MVP you dont use Context or anything else from the Android SDK/Framework in the Presenter (P) layer! This layer is for anything else than Android related stuff.

1) how to access shared preferences inside presenter.

You don't. If you need a value from a SharedPrefences in the Presenter then you could pass the value to the Presenter via a method call.

Example:

class MainActivity{
 String birthday = SharedPrefence.getString(..);
 presenter.setSavedBirtday(birthday);

 }

2) how to access other system services inside presenter.

As metioned before; You don't acesss System services in the Presenter. What you can do is call the a System Service from the Presenter.

Example with Vibrator:

1 - Create an interface:

interface OnSystemServiceCaller{
  onVibratorCall();
}

2 - Implement it in a Activity

class MainActivity implements OnSystemServiceCaller{

@Override
onVibratorCall(){
  Vibrator v = (Vibrator) getSystemService(VIBRATOR);
  v.vibrate(50);

  } 
}

3 - Call from presenter

class Presenter{
OnSystemServiceCaller listener;

  public void ifButtonClicked(){
    listener.onVibrateCall();
  }

}

3) if i am working on SQLite Databases then during any transaction in my database which is done by call from presenter to my SQLite Helper class need context to access database.

Some wont like this answers other will, this is just a suggestion.

You can access your SQLite by using a global ApplicationContext() in your app class (Class that extend Application; see how here since your SQLlite is global for the whole app and not just a particular Activity And when you need to pass data from SQLite to a Activity then you pass it first to the Presenter and from Presenter to your Activity the same way we send a call to our Vibrator method

  • @saksham for global context see here: https://stackoverflow.com/a/9445685/5366495. The road to updating the database from lets say a value you get from an `Edittext` can be like this: `Edittext.gettext() -> pass text to Presenter -> from Presenter -> To your Databse layer`. I will also suggest taking a look at `RealmDatabases` which will help you in much of your problem and makes everything a lot esier! –  Jun 24 '17 at 12:25
  • I know how to work on realm databases. I have done projects on realm, as realm do not need context so it's quite easy to implement with MVP but one of my project is totally based on sql lite so i need solution for that – saksham Jun 24 '17 at 12:27
  • Is is safe to store ApplicationContext() in static field? – saksham Jun 24 '17 at 12:45
  • @saksham Yes in that case with SQLlite it is because it is used globally in your app –  Jun 25 '17 at 11:16