Comments (4)
It is a good practice in the MVP pattern to leave the Presenter plain old Java as much as possible. It makes it much easier to test the presenter without the need of Android Tests or Robolectric for example.
If you just want to initialize the presenter with the data, you could read the intent by getIntent()
in the providePresenter()
of the Activity and pass the data into the Presenter's Constructor.
If you want to "update" the presenter data by Intent data, provide a method in the presenter for it and call it with the data e.g. in onCreate()
.
from thirtyinch.
This is from the sample project:
@NonNull
@Override
public AddEditTaskPresenter providePresenter() {
final String taskId = getIntent()
.getStringExtra(AddEditTaskActivity.ARGUMENT_EDIT_TASK_ID);
final TasksRepository tasksRepository = Injection
.provideTasksRepository(getApplicationContext());
return new AddEditTaskPresenter(taskId, tasksRepository);
}
from thirtyinch.
Thank you, that is a good idea, but since I'm using dagger2 for presenter dependencies, I had to use:
@Inject Provider<MyPresenter> presenterFactory
then I used the graph to inject it in providePresenter
method. The dependecies are passed by injecting the presenter's constructor for ease of testing. So in the end I had to make a setter method on the presenter to set the state on the activity's onCreate. Am I doing this right? I thought it was wrong for the activity to do any actions, it should only be a 'passive' view displayer?
Thanks for all your answers!
from thirtyinch.
I never use dagger to inject the presenter. I don't think it's useful. You never exchange the presenter with a mock implementation when doing tests. So my Activities and Presenters are hardcoded glued together.
On the other hand using Dagger to inject dependnecies inside you presenter could be useful. If you're starting a new project consider building your app with constructor injection instead of dagger. It's often easier and has a good separation; inject android dependencies in your Activity and mock dependencies in your tests.
On android the passive view is often not possible. It is possible for your view implementation but the Activity
has functionality which goes beyond the functionality of a view. Initialising the presenter, starting other Activities and handle the ActivityResult are things which aren't view related. But there is no other way to do it on Android
Don't over engineer and get things done in a testable way where it is testable, that's probably my best tip
from thirtyinch.
Related Issues (20)
- Debugging a @CallOnMainThread annotated method crashes the app HOT 2
- Create Kotlin module/extensions HOT 2
- Switch from compileOnly to implementation HOT 1
- [Discussion] Should manageViewDisposable() throw an exception
- Support for AndroidX HOT 4
- BackstackReader doesn't work with AndroidX HOT 3
- "Don't add observers when the presenter reached the DESTROYED state" EXCEPTION HOT 1
- sendToView HOT 4
- Lint warning with sendToView, but not with deliverToView
- Why saving the Presenter on orientation change HOT 1
- Problems with @CallOnMainThread
- Lint does not report by abstract classes without implementation View
- Crash using AndroidX with ProGuard enabled HOT 2
- Lint reports: Obsolete custom lint check
- bundle[buildType] was replaced with AGP 3.2.0
- Package thirtyinch-kotlin-coroutines not published HOT 3
- Plugin module not available with version 0.9.5 HOT 2
- Release for androidx HOT 2
- Restore old BackstackReader.isInBackstack() implementation once AndroidX Fragment 1.2.1 is released HOT 1
- Migrate away from JCenter HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from thirtyinch.