benoitletondor / Android-Studio-MVP-template

Android MVP template for Android Studio
Apache License 2.0
628 stars 121 forks source link

mPresenter ,mView = null in onActivityResult #15

Closed TieuNhi95 closed 6 years ago

TieuNhi95 commented 6 years ago

thanks for your template but i have a problem with mPresenter and mView sometime ,its return null in onActivityResult.

benoitletondor commented 6 years ago

Hi @TieuNhi95 ,

Yes, it's unfortunate but your Activity is not started when reaching onActivityResult, meaning that your mView variable will be null into your Presenter.

You should always ensure you handle those cases with something like that :

public class MyPresenter extends BasePresenterImpl {
     @Nullable
     private Stuff mTempResult;

     @Override
     public void onStart(boolean viewCreated) {
          // ... Your usual code here

          if( mTempResult != null ) {
               // Handle your result
               mTempResult = null;
          }
     }

     public void onOtherActivityResult(@NonNull Stuff result) {
           if( mView != null ) {
                 // Handle your result directly here
           } else {
                 mTempResult = result;
           }
     }
}

This way you ensure that your result will be handled either right away if the view is available, or at the next start otherwise.

Let me know if it's unclear.

TieuNhi95 commented 6 years ago

thanks, i got it (y)