Android MVP: safe use Context in Presenter

前端 未结 4 693
悲&欢浪女
悲&欢浪女 2021-01-17 10:02

In my app I work with ContentProvider and use LoaderManager.LoaderCallbacks.

Fragment (View)



        
4条回答
  •  野的像风
    2021-01-17 10:25

    Just don't register your presenter as Android specific callback target (e.g. BroadcastReceiver, LoaderManager.LoaderCallbacks etc.). Handle the callback methods in your View (Fragment or Activity) and pass all related data to the presenter.

    If you need Context for object creation, let your view create this object (as it has a reference to the Context). In your case the call

    Loader loader = new ArticleCatalogLoader(context, categoryId)
    

    should be refactored to

    view.createLoaderForCategory(categoryId)
    

提交回复
热议问题