sockeqwe / mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
http://hannesdorfmann.com/mosby/
Apache License 2.0
5.49k stars 841 forks source link

Defer attachView for MvpActivity until onResume? #131

Closed DrewCarlson closed 8 years ago

DrewCarlson commented 8 years ago

I may be missing something here but since onCreate will instantiate the Presenter and immediately call attachView, the Activity will not have had the chance to call setContentView. This would cause any calls to your MvpView that have a findViewById to fail when called in attachView.

sockeqwe commented 8 years ago

yes this is correct. Maybe onPostCreate() is better suited than onResume(). However, people are used to call presenter methods like presenter.loadData() in Activity.onCreate().

So they would have to move that to onPostCreate() too. You see, you make the other half of users happy by disapointing the others. Furthermore this is a breaking change.

So what I typically do is something like this:

class MyActivity extends MvpActivity {

    public void onCreate(Bundle b) {
           setContentView(R.layout.my_layout);
           // findViewById() etc.

           super.onCreate(b);
     }
}

Call super.onCreate() after having set ContentView. Alternatively, if you really want to have presenter.attachView() called in onResume() you could write your own MvpDelegate that does that in onResume(). Checkout Mosby's github pages for more information about that.

What do you think?

DrewCarlson commented 8 years ago

If this is intended behavior then any of these solutions are fine with me. I have been using your first solution of having an initialization method on the presenter that gets called in onCreate().

My issue with that is that it's only required for Activities, which (though small) is a difference I have to keep track of between Fragments and Views.

The second approach you provided seems like the best option for me.

Thank you for your insight.