How to handle onActivityResult () with a solution

I want to integrate the application with the Google Play and Mortar services to get the user's current location.

https://developer.android.com/training/location/retrieve-current.html#CheckServices

This requires processing onActivityResult () in case the Google Play services are unavailable.

My first instinct on how to do this is to make getFirstScreen() in Main.java return a blank loading screen. https://github.com/square/mortar/blob/master/mortar-sample/src/main/java/com/example/mortar/core/Main.java

After the injection during onCreate() check to see if Google Play services are available. If so, then call flow.goTo(<location using screen>) , if not, do nothing and wait onActivityResult() . Then, when onActivityResult() fires, just call flow.goTo(<location using screen>) .

The above seems a bit hacked to me. (Let me know if you need clarification). Therefore, I think that another solution could be something similar to this Mortar + Flow with third-party libraries connected to the activity life cycle and connecting onActivityResult() to the host. The problem is that I will not have access to the Activity from the host, which makes it impossible to call GooglePlayServicesUtil.getErrorDialog(...) because it requires an Activity .

I think onActivityResult() very important. Perhaps this should be part of the Mortar library?

+4
source share
1 answer

Here we usually deal with startActivityForResult in square register.

 public SomePresenter extends Presenter<SomePresenter.Activity> { public interface Activity { void startActivityForResult(android.content.Intent intent, int requestCode); } public final void onActivityResult(int requestCode, int resultCode, Intent data) { // Make sure it the expected requestCode and resultCode and do your thing. // If it isn't, no-op, someone else will handle it. } } 

And the action looks something like this:

 public MyActivity extends Activity implements SomePresenter.Activity { @Override protected void onCreate(Bundle bundle) { // Do the usual mortar init stuff somePresenter.takeView(this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); somePresenter.onActivityResult(requestCode, resultCode, data); } @Override protected void onDestroy() { somePresenter.dropView(this); super.onDestroy(); } } 

It does not help you with the error dialog, but it sounds like a separate concern for me. It looks like you can use a pair of Popup / PopupPresenter. (I'm not happy with Popup, but it does its job until we have a better idea.) Or maybe the activity should just go ahead and do it? I am not very good at the Services, I have not dealt with them yet.

+9
source

Source: https://habr.com/ru/post/1205378/


All Articles