Sample Android app that displays a list of movies using iTunes search api
Libraries and tools:
- JDK 1.8
- Android SDK
- Latest Android SDK Tools and build tools
The architecture of this app is based on the MVP (Model View Presenter) pattern
Imagine you have to implement a sign in screen.
-
Create a new package under
ui
calledsignin
-
Create a new Activity called
SignInActivity
class that extendsBaseActivity
. You could also use a Fragment -
Define the view and presenter interfaces that your Activity/Fragment and Presenter is going to implement. Create a new interface called
SignInContract
, inside create another interface calledView
that extendsBaseView
and another one calledPresenter
that extendsBasePresenter<View>
. Add the methods that you think will be necessary on both interfaces, e.g.showSignInSuccessful()
for theView
andsignIn(String email)
for thePresenter
. Code should look like this:interface SignInContract { interface View extends BaseView { void showSignInSuccessful(); } interface Presenter extends BasePresenter<View> { void signIn(String email); } }
-
Create a
SignInPresenter
class that extendsBasePresenter<SignInContract.View>
and implementsSignInContract.Presenter
-
Implement the methods in
SignInPresenter
that your Activity requires to perform the necessary actions, e.g.signIn(String email)
. Once the sign in action finishes you should callgetView().showSignInSuccessful()
-
Make your
SignInActivity
implementSignInContract.View
and implement the required methods likeshowSignInSuccessful()
-
In your activity, inject a new instance of
SignInPresenter
and callsuper.attachPresenter(presenter, this)
fromonCreate
. This will allow your presenter to have an instance of your activity and it would also let your Presenter have a lifecycle awareness, this way you can overrideonStart
,onResume
,onPause
,onStop
andonDestroy
methods on your Presenter. Also, set up a click listener in your button that callspresenter.signIn(email)
Copyright 2019 Kenneth Lopez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.