In mvp we save that reference of an activity in a weak reference.
WeakReference<Activity> view = new WeakReference<Activity>(activity);
If we lose the reference. can we get it back?
3 Answers
If you lose the reference to your Activity
it means the activity was garbage collected and it doesn't exist anymore. You can't get back what doesn't exist.
Ex. If this happens because of configuration change it means a new activity
was created.
You need a way to attach the newly created view to the same presenter.
If you are looking for libraries to help you, take a look at mosby and nucleus.
-
"You need a way to attach the newly created view" how to do so? thx so much for your answer Sep 8, 2015 at 8:47
-
1@MethnaniBilel it's a complex problem to solve... take a look at mosby and/or nucles to see how they have solved it. Sep 8, 2015 at 8:49
I don't think you should be saving a reference to an Activity
in MVP
at all - doesn't matter if it's hard or weak!
I'm assuming you're storing this reference in the Presenter
. To really decouple the layers you should create an interface that describes your View
(Activity
) and use it instead of the activity.
So you'd do:
public interface LoginView {
displayUsernameError(String error);
displayPasswordError(String error);
openMainScreen();
}
Your Activity
should implement the interface from above.
public class LoginActivity implements LoginView {
...
}
In your presenter you'd have:
class LoginPresenter {
private LoginView mView;
public LoginPresenter(LoginView view) {
mView = view;
}
public onLoginButtonClicked(String username, char[] password) {
...
mView.openMainScreen();
}
}
Immediate benefits of doing this:
The different layers are really decoupled. You can change your
Activity
(say you decide to useFragments
instead) without touching yourPresenter
.Your presenter is fully testable using
JUnit
only! No need to use anything fancy to verify your interactions are correct, just plainMockito
to mock theLoginView
.
One other point to note - are you sure you want your Presenter
to outlive your View
? There are some situations when that can't be avoided, but in most cases they have the same life span - when the View
is destroyed the Presenter
should be as well.
-
3
-
"I don't think you should be saving a reference to an Activity in MVP at all - doesn't matter if it's hard or weak!" you do understand
private LoginView mView;
is already a reference, right?– FaridJan 17 at 20:04
How did you set the reference in the first place?
You should be setting it with a setter method in Activity's onCreate. This "setter" method is often called "attach" or "bind".
fun attach(view: View) {
this.view = view
}
So when new Activity is created due to configuration change, it will set itself to the presenter again. Note that you might be dealing with a new instance of the presenter too. However, based on your question I believe you want to attach the newly created activity to the same instance of presenter. If you got your presenter scoped right, this will work for both cases :)