Mortar + Flow with third party libraries hooked to activity lifecycle

前端 未结 2 650
心在旅途
心在旅途 2021-01-30 18:52

Some third party libraries use hooks into the activity lifecycle to work correctly - for instance, the Facebook SDK (https://developers.facebook.com/docs/android/login-with-face

2条回答
  •  遥遥无期
    2021-01-30 19:14

    So I've been porting a personal app over to flow and mortar to evaluate it for businesses use. I haven't encountered a scenario where I HAD to have the entire activity lifecycle yet, but as things stand with the current version of flow (v0.4) & mortar (v0.7), this is something I think you will have to creatively solve for yourself. I've recognized this as a potential problem for myself and have put some thought of how to overcome it:

    I would also like to note that I haven't actually used the Facebook SDK. You will have to choose the best method for yourself.

    1. You could post events from the activity for each Activity life cycle event. You essentially mentioned this approach using RXJava's Observables. If you really really wanted to use RXJava, you could use a PublishSubject for this, but I'd probably go with simple events from an EventBus you could subscribe to. This is probably the easiest approach.
    2. You could also, depending on how the Facebook SDK works, possibly inject the Facebook SDK component in the activity, and from there initialize it. Then also inject the Facebook SDK component into your view to be used. Flow and Mortar's entire system is deeply integrated into dependency injection after all? This approach is also fairly simple, but depending on how the Facebook SDK works it probably isn't the best option. If you did go this route, you'd need to heed my warning at the bottom of this post.
    3. This brings us to my last idea. Square had a similar problem when they needed access to an Activity's ActionBar in it's sub-views/presenters. They exposed access to the ActionBar in their sample app via something they called the ActionBarOwner.java. They then implement the ActionBarOwner interface and give an instance of itself in the DemoActivity.java. If you study how they implemented this and share it through injection, you could create a similar class. AcivityLifecycleOwner or something (the name needs work), and you could subscribe to callbacks on it from a presenter. If you decide to go down this route, and aren't careful you can easily end up with a memory leak. Any time you would subscribe to any of the events (I'd recommend you subscribe in the presenter), you'd need to make sure you unsubscribe in the onDestroy method as well. I've created a short untested sample of what I mean for this solution below.

    No matter which approach you use, you'll probably need to make sure your onCreate and onDestroy methods actually come from your presenter, and not the exact events from the activity. If you are only using the sdk on a single view, the activity's onCreate has been called long before your view is instantiated probably, and the onDestroy for the Activity will be called after your view is destroyed. The presenter's onLoad and onDestroy should suffice I think, however I haven't tested this.

    Best of luck!

    Untested code example for solution #3:

    All your presenters could extend this class instead of ViewPresenter and then override each method you wanted events for just like you would in an activity:

    public abstract class ActivityLifecycleViewPresenter extends ViewPresenter
        implements ActivityLifecycleListener {
    
      @Inject ActivityLifecycleOwner mActivityLifecycleOwner;
    
      @Override protected void onLoad(Bundle savedInstanceState) {
        super.onLoad(savedInstanceState);
        mActivityLifecycleOwner.register(this);
      }
    
      @Override protected void onDestroy() {
        super.onDestroy();
        mActivityLifecycleOwner.unregister(this);
      }
    
      @Override public void onActivityResume() {
      }
    
      @Override public void onActivityPause() {
      }
    
      @Override public void onActivityStart() {
      }
    
      @Override public void onActivityStop() {
      }
    
    }
    

    Activity Lifecycle owner that would be injected into the activity and then hooked up to the corresponding events. I purposely didn't include onCreate and onDestroy, as you presenter's wouldn't be able to get access to those events as they wouldn't be created or they would already be destroyed. You'd need to use the presenters onLoad and onDestroy methods in place of those. It's also possible that some of these other events wouldn't be called.

    public class ActivityLifecycleOwner implements ActivityLifecycleListener {
    
      private List mRegisteredListeners
          = new ArrayList();
    
      public void register(ActivityLifecycleListener listener) {
        mRegisteredListeners.add(listener);
      }
    
      public void unregister(ActivityLifecycleListener listener) {
        mRegisteredListeners.remove(listener);
      }
    
      @Override public void onActivityResume() {
        for (ActivityLifecycleListener c : mRegisteredListeners) {
          c.onActivityResume();
        }
      }
    
      @Override public void onActivityPause() {
        for (ActivityLifecycleListener c : mRegisteredListeners) {
          c.onActivityPause();
        }
      }
    
      @Override public void onActivityStart() {
        for (ActivityLifecycleListener c : mRegisteredListeners) {
          c.onActivityStart();
        }
      }
    
      @Override public void onActivityStop() {
        for (ActivityLifecycleListener c : mRegisteredListeners) {
          c.onActivityStop();
        }
      }
    }
    

    Now you need to hook the lifecycle owner to the activity:

    public class ActivityLifecycleExample extends MortarActivity {
    
      @Inject ActivityLifecycleOwner mActivityLifecycleOwner;
    
      @Override protected void onResume() {
        super.onResume();
        mActivityLifecycleOwner.onActivityResume();
      }
    
      @Override protected void onPause() {
        super.onPause();
        mActivityLifecycleOwner.onActivityPause();
      }
    
      @Override protected void onStart() {
        super.onStart();
        mActivityLifecycleOwner.onActivityStart();
      }
    
      @Override protected void onStop() {
        super.onStart();
        mActivityLifecycleOwner.onActivityStop();
      }
    
    }
    

提交回复
热议问题