简体   繁体   English

如何使用Mortar处理onActivityResult()

[英]How to handle onActivityResult() with Mortar

I am looking to integrate an application with Google Play Services and Mortar to retrieve the user's current location. 我希望将应用程序与Google Play服务和Mortar集成,以检索用户的当前位置。

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

This requires handling onActivityResult() in the case that Google Play Services is not available. 这需要在Google Play服务不可用的情况下处理onActivityResult()。

My first instinct on how to do this is to make getFirstScreen() in Main.java return a blank loading screen. 我对如何做到这一点的第一直觉是让Main.java getFirstScreen()返回一个空白加载屏幕。 https://github.com/square/mortar/blob/master/mortar-sample/src/main/java/com/example/mortar/core/Main.java https://github.com/square/mortar/blob/master/mortar-sample/src/main/java/com/example/mortar/core/Main.java

After injection, during onCreate() perform the check to see if Google Play Services is available. 注入后,在onCreate()期间执行检查以查看Google Play服务是否可用。 If it is, then call flow.goTo(<location using screen>) , if not, do nothing and wait for onActivityResult() to be called. 如果是,则调用flow.goTo(<location using screen>) ,如果没有,则不执行任何操作并等待onActivityResult()被调用。 Then, when onActivityResult() fires, simply call flow.goTo(<location using screen>) . 然后,当onActivityResult()触发时,只需调用flow.goTo(<location using screen>)

The above seems slightly hacky to me. 以上看起来对我来说有些苛刻。 (Let me know if you need clarification). (如果您需要澄清,请告诉我)。 So I'm thinking the other solution might be to do something similar to this Mortar + Flow with third party libraries hooked to activity lifecycle and hook up onActivityResult() to the presenter. 所以我认为另一个解决方案可能是做一些类似于这个Mortar + Flow的东西,第三方库挂钩到活动生命周期,并将onActivityResult()连接到演示者。 The problem with this is that I won't have access to the Activity from a presenter which makes it impossible to call GooglePlayServicesUtil.getErrorDialog(...) because it requires Activity . 这样做的问题是我无法从演示者访问Activity ,这使得无法调用GooglePlayServicesUtil.getErrorDialog(...)因为它需要Activity

I think onActivityResult() is very important. 我认为onActivityResult()非常重要。 Perhaps it should be a part of the Mortar library? 也许它应该是Mortar库的一部分?

Here's how we generally deal with startActivityForResult in Square Register. 以下是我们如何处理Square Register中的startActivityForResult。

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's the expected requestCode and resultCode and do your thing.
    // If it isn't, no-op, someone else will handle it.
  }
}

And the activity looks something like: 活动看起来像:

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();
  }
}

That doesn't help you with the error dialog, but that sounds like a separate concern to me anyway. 这对错误对话框没有帮助,但这对我来说听起来像是一个单独的问题。 Seems like you can use a Popup / PopupPresenter pair there. 好像你可以在那里使用Popup / PopupPresenter对。 (I'm not thrilled with Popup, but it gets the job done until we have a better idea.) Or maybe the activity should just go ahead and deal with it itself? (我对Popup并不感到兴奋,但是在我们有了更好的想法之前它就完成了工作。)或者也许活动应该继续并自己处理它? I'm not super familiar with the Play Services, haven't dealt with them yet. 我对Play服务并不是很熟悉,还没有处理过它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM