how to start commit Fragment in FirebaseMessagingService

后端 未结 3 2013
感情败类
感情败类 2021-01-27 08:27

I used FireBase could messaging in my application and I want when the user receive message the activity fragment change. I did the following code for that but i don\'t know why

相关标签:
3条回答
  • 2021-01-27 08:40

    You should broadcast any firebase message updates and register broadcast receiver that handles that updates(where you should do fragment transactions) in your activity. If your activity is in the foreground that means luckily your receiver is registered. If you want to add the fragment in firebase service context, you can implement your own android.app.Application.ActivityLifecycleCallbacks interface, like

    public class ActivityLifeCycleHandler implements Application.ActivityLifecycleCallbacks { 
       Activity currentActivity;
       @Override
       public void onActivityResumed(Activity foregroundActivity) {
          this.currentActivity = foregroundActivity;
      }
    }
    

    And get your activity's reference from ActivityLifeCycleHandler class of yours.(be careful not to leak the activity) . I wouldn't recommend this solution thought.

    This callback is registered for your application instance and its callbacks(onResume()) are triggered by Activity life-cycle methods(super.onResume()) for every activity in your application.

    0 讨论(0)
  • 2021-01-27 08:43

    you can't get a reference to getFragmentManager() in the service class How erver you can use a broadcast to listne the calls, anywhere else in the application.

    0 讨论(0)
  • 2021-01-27 08:46

    getFragmentManager() is a method of Activity, therefore it can't work in a Service.

    0 讨论(0)
提交回复
热议问题