update recycler view while receive fcm notification










0















I want to refresh my recycler view in fragment while receive FCM notification(if specific fragment is open) else receive notification.



I have tried multiple solutions, but that didn't work.



MyFirebaseMessagingService.java



@Override
public void onMessageReceived(RemoteMessage remoteMessage)
Map<String, String> data = remoteMessage.getData();

if (remoteMessage.getNotification() != null)
String noti_body = remoteMessage.getNotification().getBody();
String noti_title = remoteMessage.getNotification().getTitle();
String noti_activity = data.get("activity");

mBuilder = new NotificationCompat.Builder(this, "M_CH_ID")
.setSmallIcon(R.mipmap.logo)
.setContentTitle(noti_title)
.setContentText(noti_body)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo))
.setColor(getResources().getColor(R.color.colorAccent))
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setAutoCancel(true); // clear notification after click

if (remoteMessage.getData().size() > 0)
fragmentNewJobs c = new fragmentNewJobs();
if (c.getUserVisibleHint()) //fragmentNewJobs.is_open
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("activity", noti_activity);
getApplicationContext().startActivity(intent);
else
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("activity", noti_activity);

@SuppressLint("WrongConstant")
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());







fragmentNewJobs c = new fragmentNewJobs();
if (c.getUserVisibleHint())




I have tried this, but it load fragmentNewJobs.java even I have opened any screen.




  1. I want to refresh recyclerView in fragmentNewJobs.java only if fragmentNewJobs is open else receive notification.



    1. I want to add only new item to recyclerView(not load all data if possible)










share|improve this question




























    0















    I want to refresh my recycler view in fragment while receive FCM notification(if specific fragment is open) else receive notification.



    I have tried multiple solutions, but that didn't work.



    MyFirebaseMessagingService.java



    @Override
    public void onMessageReceived(RemoteMessage remoteMessage)
    Map<String, String> data = remoteMessage.getData();

    if (remoteMessage.getNotification() != null)
    String noti_body = remoteMessage.getNotification().getBody();
    String noti_title = remoteMessage.getNotification().getTitle();
    String noti_activity = data.get("activity");

    mBuilder = new NotificationCompat.Builder(this, "M_CH_ID")
    .setSmallIcon(R.mipmap.logo)
    .setContentTitle(noti_title)
    .setContentText(noti_body)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo))
    .setColor(getResources().getColor(R.color.colorAccent))
    .setDefaults(Notification.DEFAULT_ALL)
    .setPriority(NotificationManager.IMPORTANCE_HIGH)
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    .setAutoCancel(true); // clear notification after click

    if (remoteMessage.getData().size() > 0)
    fragmentNewJobs c = new fragmentNewJobs();
    if (c.getUserVisibleHint()) //fragmentNewJobs.is_open
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("activity", noti_activity);
    getApplicationContext().startActivity(intent);
    else
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("activity", noti_activity);

    @SuppressLint("WrongConstant")
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    mBuilder.setContentIntent(pi);
    NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());







    fragmentNewJobs c = new fragmentNewJobs();
    if (c.getUserVisibleHint())




    I have tried this, but it load fragmentNewJobs.java even I have opened any screen.




    1. I want to refresh recyclerView in fragmentNewJobs.java only if fragmentNewJobs is open else receive notification.



      1. I want to add only new item to recyclerView(not load all data if possible)










    share|improve this question


























      0












      0








      0








      I want to refresh my recycler view in fragment while receive FCM notification(if specific fragment is open) else receive notification.



      I have tried multiple solutions, but that didn't work.



      MyFirebaseMessagingService.java



      @Override
      public void onMessageReceived(RemoteMessage remoteMessage)
      Map<String, String> data = remoteMessage.getData();

      if (remoteMessage.getNotification() != null)
      String noti_body = remoteMessage.getNotification().getBody();
      String noti_title = remoteMessage.getNotification().getTitle();
      String noti_activity = data.get("activity");

      mBuilder = new NotificationCompat.Builder(this, "M_CH_ID")
      .setSmallIcon(R.mipmap.logo)
      .setContentTitle(noti_title)
      .setContentText(noti_body)
      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo))
      .setColor(getResources().getColor(R.color.colorAccent))
      .setDefaults(Notification.DEFAULT_ALL)
      .setPriority(NotificationManager.IMPORTANCE_HIGH)
      .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
      .setAutoCancel(true); // clear notification after click

      if (remoteMessage.getData().size() > 0)
      fragmentNewJobs c = new fragmentNewJobs();
      if (c.getUserVisibleHint()) //fragmentNewJobs.is_open
      Intent intent = new Intent(this, MainActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.putExtra("activity", noti_activity);
      getApplicationContext().startActivity(intent);
      else
      Intent intent = new Intent(this, MainActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.putExtra("activity", noti_activity);

      @SuppressLint("WrongConstant")
      PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
      mBuilder.setContentIntent(pi);
      NotificationManager mNotificationManager =
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      mNotificationManager.notify(0, mBuilder.build());







      fragmentNewJobs c = new fragmentNewJobs();
      if (c.getUserVisibleHint())




      I have tried this, but it load fragmentNewJobs.java even I have opened any screen.




      1. I want to refresh recyclerView in fragmentNewJobs.java only if fragmentNewJobs is open else receive notification.



        1. I want to add only new item to recyclerView(not load all data if possible)










      share|improve this question
















      I want to refresh my recycler view in fragment while receive FCM notification(if specific fragment is open) else receive notification.



      I have tried multiple solutions, but that didn't work.



      MyFirebaseMessagingService.java



      @Override
      public void onMessageReceived(RemoteMessage remoteMessage)
      Map<String, String> data = remoteMessage.getData();

      if (remoteMessage.getNotification() != null)
      String noti_body = remoteMessage.getNotification().getBody();
      String noti_title = remoteMessage.getNotification().getTitle();
      String noti_activity = data.get("activity");

      mBuilder = new NotificationCompat.Builder(this, "M_CH_ID")
      .setSmallIcon(R.mipmap.logo)
      .setContentTitle(noti_title)
      .setContentText(noti_body)
      .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo))
      .setColor(getResources().getColor(R.color.colorAccent))
      .setDefaults(Notification.DEFAULT_ALL)
      .setPriority(NotificationManager.IMPORTANCE_HIGH)
      .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
      .setAutoCancel(true); // clear notification after click

      if (remoteMessage.getData().size() > 0)
      fragmentNewJobs c = new fragmentNewJobs();
      if (c.getUserVisibleHint()) //fragmentNewJobs.is_open
      Intent intent = new Intent(this, MainActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.putExtra("activity", noti_activity);
      getApplicationContext().startActivity(intent);
      else
      Intent intent = new Intent(this, MainActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.putExtra("activity", noti_activity);

      @SuppressLint("WrongConstant")
      PendingIntent pi = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
      mBuilder.setContentIntent(pi);
      NotificationManager mNotificationManager =
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      mNotificationManager.notify(0, mBuilder.build());







      fragmentNewJobs c = new fragmentNewJobs();
      if (c.getUserVisibleHint())




      I have tried this, but it load fragmentNewJobs.java even I have opened any screen.




      1. I want to refresh recyclerView in fragmentNewJobs.java only if fragmentNewJobs is open else receive notification.



        1. I want to add only new item to recyclerView(not load all data if possible)







      java android firebase service notifications






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 8:29









      KENdi

      5,7992821




      5,7992821










      asked Nov 14 '18 at 5:14









      Asif UllahAsif Ullah

      249




      249






















          2 Answers
          2






          active

          oldest

          votes


















          1














          You can use Event Bus to publish message from receiver to particular fragment or activity.



          And to findout if activity is visible or not have a look at this



          Or if you are using fragment you can use static variable in Application class to track fragment(Most basic use)






          share|improve this answer























          • thanks @taman. Broadcast works perfectly. but (on pause , on stop , onDestroy etc )is not called when i switch to other activity or fragment. i am using it in fragment.

            – Asif Ullah
            Nov 14 '18 at 6:10






          • 1





            If you are using broadcast make sure to check out changes in marshmello for broadcast. (Broadcast receiver must be registered and unregistered from code not only from manifest)

            – taman neupane
            Nov 14 '18 at 6:12











          • yes. broadcast is working perfectly. only issue is how to unregistered when fragment state changes? or fragment is not active/visible?

            – Asif Ullah
            Nov 14 '18 at 6:28


















          1















          i want to add only new item to recycler view(not load all data if
          possible)




          • You can use BroadcastManager to send notification event to your existing Activity. Running Activity will add new item in List when it receive new broadcast.

          • Another option to notify Activity can be EventBus, that is fast growing, easy implementation library for notifying between Services, Activities and Fragments.

          How to use BroadcastManager?



          How to use EventBus?




          i want to refresh recyclerview in fragmentNewJobs.java only if
          fragmentNewJobs is open else receive notification.




          For this you can take a static boolean in Application class, and make it true on Fragment's onResume() and make it false on onPause().



          Then you can check Fragment's visibility by ApplicationClass.isFragmentVisible()



          Edit



          You can change fragmentVisible boolean inside setUserVisibleHint() of Fragment, that will work perfectly. You will need to make initially UserVisibleHint false because it is true default. Check this answer for better understanding.






          share|improve this answer

























          • Broadcast works perfectly. but on pause is not called when i switch to other activity or fragment.

            – Asif Ullah
            Nov 14 '18 at 6:08






          • 1





            Fragment onPause() will always called when user is going to other Screen, either Activity or Fragment. Did you set logs in onPause() of fragment?

            – Khemraj
            Nov 14 '18 at 6:13











          • yes. i have override all methods like onPause, Onstop, OnDestroy etc.. I have multiple Fragments in MainActivity and in fragment i have view pager and set 3 tabs. if swipe to 3rd tab. then onPause calls but if change fragment from navigaton drawer then nothing is called. @Override public void onPause() is_open = false; Log.d( "receiversender: ", "on pause"); super.onPause();

            – Asif Ullah
            Nov 14 '18 at 6:21






          • 1





            That is not called because fragment is inside another parent fragment, so it is not destroyed. It just goes in stack.

            – Khemraj
            Nov 14 '18 at 6:29







          • 1





            Yes, check edit part of answer.

            – Khemraj
            Nov 14 '18 at 6:33










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293571%2fupdate-recycler-view-while-receive-fcm-notification%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can use Event Bus to publish message from receiver to particular fragment or activity.



          And to findout if activity is visible or not have a look at this



          Or if you are using fragment you can use static variable in Application class to track fragment(Most basic use)






          share|improve this answer























          • thanks @taman. Broadcast works perfectly. but (on pause , on stop , onDestroy etc )is not called when i switch to other activity or fragment. i am using it in fragment.

            – Asif Ullah
            Nov 14 '18 at 6:10






          • 1





            If you are using broadcast make sure to check out changes in marshmello for broadcast. (Broadcast receiver must be registered and unregistered from code not only from manifest)

            – taman neupane
            Nov 14 '18 at 6:12











          • yes. broadcast is working perfectly. only issue is how to unregistered when fragment state changes? or fragment is not active/visible?

            – Asif Ullah
            Nov 14 '18 at 6:28















          1














          You can use Event Bus to publish message from receiver to particular fragment or activity.



          And to findout if activity is visible or not have a look at this



          Or if you are using fragment you can use static variable in Application class to track fragment(Most basic use)






          share|improve this answer























          • thanks @taman. Broadcast works perfectly. but (on pause , on stop , onDestroy etc )is not called when i switch to other activity or fragment. i am using it in fragment.

            – Asif Ullah
            Nov 14 '18 at 6:10






          • 1





            If you are using broadcast make sure to check out changes in marshmello for broadcast. (Broadcast receiver must be registered and unregistered from code not only from manifest)

            – taman neupane
            Nov 14 '18 at 6:12











          • yes. broadcast is working perfectly. only issue is how to unregistered when fragment state changes? or fragment is not active/visible?

            – Asif Ullah
            Nov 14 '18 at 6:28













          1












          1








          1







          You can use Event Bus to publish message from receiver to particular fragment or activity.



          And to findout if activity is visible or not have a look at this



          Or if you are using fragment you can use static variable in Application class to track fragment(Most basic use)






          share|improve this answer













          You can use Event Bus to publish message from receiver to particular fragment or activity.



          And to findout if activity is visible or not have a look at this



          Or if you are using fragment you can use static variable in Application class to track fragment(Most basic use)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 5:22









          taman neupanetaman neupane

          675516




          675516












          • thanks @taman. Broadcast works perfectly. but (on pause , on stop , onDestroy etc )is not called when i switch to other activity or fragment. i am using it in fragment.

            – Asif Ullah
            Nov 14 '18 at 6:10






          • 1





            If you are using broadcast make sure to check out changes in marshmello for broadcast. (Broadcast receiver must be registered and unregistered from code not only from manifest)

            – taman neupane
            Nov 14 '18 at 6:12











          • yes. broadcast is working perfectly. only issue is how to unregistered when fragment state changes? or fragment is not active/visible?

            – Asif Ullah
            Nov 14 '18 at 6:28

















          • thanks @taman. Broadcast works perfectly. but (on pause , on stop , onDestroy etc )is not called when i switch to other activity or fragment. i am using it in fragment.

            – Asif Ullah
            Nov 14 '18 at 6:10






          • 1





            If you are using broadcast make sure to check out changes in marshmello for broadcast. (Broadcast receiver must be registered and unregistered from code not only from manifest)

            – taman neupane
            Nov 14 '18 at 6:12











          • yes. broadcast is working perfectly. only issue is how to unregistered when fragment state changes? or fragment is not active/visible?

            – Asif Ullah
            Nov 14 '18 at 6:28
















          thanks @taman. Broadcast works perfectly. but (on pause , on stop , onDestroy etc )is not called when i switch to other activity or fragment. i am using it in fragment.

          – Asif Ullah
          Nov 14 '18 at 6:10





          thanks @taman. Broadcast works perfectly. but (on pause , on stop , onDestroy etc )is not called when i switch to other activity or fragment. i am using it in fragment.

          – Asif Ullah
          Nov 14 '18 at 6:10




          1




          1





          If you are using broadcast make sure to check out changes in marshmello for broadcast. (Broadcast receiver must be registered and unregistered from code not only from manifest)

          – taman neupane
          Nov 14 '18 at 6:12





          If you are using broadcast make sure to check out changes in marshmello for broadcast. (Broadcast receiver must be registered and unregistered from code not only from manifest)

          – taman neupane
          Nov 14 '18 at 6:12













          yes. broadcast is working perfectly. only issue is how to unregistered when fragment state changes? or fragment is not active/visible?

          – Asif Ullah
          Nov 14 '18 at 6:28





          yes. broadcast is working perfectly. only issue is how to unregistered when fragment state changes? or fragment is not active/visible?

          – Asif Ullah
          Nov 14 '18 at 6:28













          1















          i want to add only new item to recycler view(not load all data if
          possible)




          • You can use BroadcastManager to send notification event to your existing Activity. Running Activity will add new item in List when it receive new broadcast.

          • Another option to notify Activity can be EventBus, that is fast growing, easy implementation library for notifying between Services, Activities and Fragments.

          How to use BroadcastManager?



          How to use EventBus?




          i want to refresh recyclerview in fragmentNewJobs.java only if
          fragmentNewJobs is open else receive notification.




          For this you can take a static boolean in Application class, and make it true on Fragment's onResume() and make it false on onPause().



          Then you can check Fragment's visibility by ApplicationClass.isFragmentVisible()



          Edit



          You can change fragmentVisible boolean inside setUserVisibleHint() of Fragment, that will work perfectly. You will need to make initially UserVisibleHint false because it is true default. Check this answer for better understanding.






          share|improve this answer

























          • Broadcast works perfectly. but on pause is not called when i switch to other activity or fragment.

            – Asif Ullah
            Nov 14 '18 at 6:08






          • 1





            Fragment onPause() will always called when user is going to other Screen, either Activity or Fragment. Did you set logs in onPause() of fragment?

            – Khemraj
            Nov 14 '18 at 6:13











          • yes. i have override all methods like onPause, Onstop, OnDestroy etc.. I have multiple Fragments in MainActivity and in fragment i have view pager and set 3 tabs. if swipe to 3rd tab. then onPause calls but if change fragment from navigaton drawer then nothing is called. @Override public void onPause() is_open = false; Log.d( "receiversender: ", "on pause"); super.onPause();

            – Asif Ullah
            Nov 14 '18 at 6:21






          • 1





            That is not called because fragment is inside another parent fragment, so it is not destroyed. It just goes in stack.

            – Khemraj
            Nov 14 '18 at 6:29







          • 1





            Yes, check edit part of answer.

            – Khemraj
            Nov 14 '18 at 6:33















          1















          i want to add only new item to recycler view(not load all data if
          possible)




          • You can use BroadcastManager to send notification event to your existing Activity. Running Activity will add new item in List when it receive new broadcast.

          • Another option to notify Activity can be EventBus, that is fast growing, easy implementation library for notifying between Services, Activities and Fragments.

          How to use BroadcastManager?



          How to use EventBus?




          i want to refresh recyclerview in fragmentNewJobs.java only if
          fragmentNewJobs is open else receive notification.




          For this you can take a static boolean in Application class, and make it true on Fragment's onResume() and make it false on onPause().



          Then you can check Fragment's visibility by ApplicationClass.isFragmentVisible()



          Edit



          You can change fragmentVisible boolean inside setUserVisibleHint() of Fragment, that will work perfectly. You will need to make initially UserVisibleHint false because it is true default. Check this answer for better understanding.






          share|improve this answer

























          • Broadcast works perfectly. but on pause is not called when i switch to other activity or fragment.

            – Asif Ullah
            Nov 14 '18 at 6:08






          • 1





            Fragment onPause() will always called when user is going to other Screen, either Activity or Fragment. Did you set logs in onPause() of fragment?

            – Khemraj
            Nov 14 '18 at 6:13











          • yes. i have override all methods like onPause, Onstop, OnDestroy etc.. I have multiple Fragments in MainActivity and in fragment i have view pager and set 3 tabs. if swipe to 3rd tab. then onPause calls but if change fragment from navigaton drawer then nothing is called. @Override public void onPause() is_open = false; Log.d( "receiversender: ", "on pause"); super.onPause();

            – Asif Ullah
            Nov 14 '18 at 6:21






          • 1





            That is not called because fragment is inside another parent fragment, so it is not destroyed. It just goes in stack.

            – Khemraj
            Nov 14 '18 at 6:29







          • 1





            Yes, check edit part of answer.

            – Khemraj
            Nov 14 '18 at 6:33













          1












          1








          1








          i want to add only new item to recycler view(not load all data if
          possible)




          • You can use BroadcastManager to send notification event to your existing Activity. Running Activity will add new item in List when it receive new broadcast.

          • Another option to notify Activity can be EventBus, that is fast growing, easy implementation library for notifying between Services, Activities and Fragments.

          How to use BroadcastManager?



          How to use EventBus?




          i want to refresh recyclerview in fragmentNewJobs.java only if
          fragmentNewJobs is open else receive notification.




          For this you can take a static boolean in Application class, and make it true on Fragment's onResume() and make it false on onPause().



          Then you can check Fragment's visibility by ApplicationClass.isFragmentVisible()



          Edit



          You can change fragmentVisible boolean inside setUserVisibleHint() of Fragment, that will work perfectly. You will need to make initially UserVisibleHint false because it is true default. Check this answer for better understanding.






          share|improve this answer
















          i want to add only new item to recycler view(not load all data if
          possible)




          • You can use BroadcastManager to send notification event to your existing Activity. Running Activity will add new item in List when it receive new broadcast.

          • Another option to notify Activity can be EventBus, that is fast growing, easy implementation library for notifying between Services, Activities and Fragments.

          How to use BroadcastManager?



          How to use EventBus?




          i want to refresh recyclerview in fragmentNewJobs.java only if
          fragmentNewJobs is open else receive notification.




          For this you can take a static boolean in Application class, and make it true on Fragment's onResume() and make it false on onPause().



          Then you can check Fragment's visibility by ApplicationClass.isFragmentVisible()



          Edit



          You can change fragmentVisible boolean inside setUserVisibleHint() of Fragment, that will work perfectly. You will need to make initially UserVisibleHint false because it is true default. Check this answer for better understanding.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 6:33

























          answered Nov 14 '18 at 5:19









          KhemrajKhemraj

          13.5k33878




          13.5k33878












          • Broadcast works perfectly. but on pause is not called when i switch to other activity or fragment.

            – Asif Ullah
            Nov 14 '18 at 6:08






          • 1





            Fragment onPause() will always called when user is going to other Screen, either Activity or Fragment. Did you set logs in onPause() of fragment?

            – Khemraj
            Nov 14 '18 at 6:13











          • yes. i have override all methods like onPause, Onstop, OnDestroy etc.. I have multiple Fragments in MainActivity and in fragment i have view pager and set 3 tabs. if swipe to 3rd tab. then onPause calls but if change fragment from navigaton drawer then nothing is called. @Override public void onPause() is_open = false; Log.d( "receiversender: ", "on pause"); super.onPause();

            – Asif Ullah
            Nov 14 '18 at 6:21






          • 1





            That is not called because fragment is inside another parent fragment, so it is not destroyed. It just goes in stack.

            – Khemraj
            Nov 14 '18 at 6:29







          • 1





            Yes, check edit part of answer.

            – Khemraj
            Nov 14 '18 at 6:33

















          • Broadcast works perfectly. but on pause is not called when i switch to other activity or fragment.

            – Asif Ullah
            Nov 14 '18 at 6:08






          • 1





            Fragment onPause() will always called when user is going to other Screen, either Activity or Fragment. Did you set logs in onPause() of fragment?

            – Khemraj
            Nov 14 '18 at 6:13











          • yes. i have override all methods like onPause, Onstop, OnDestroy etc.. I have multiple Fragments in MainActivity and in fragment i have view pager and set 3 tabs. if swipe to 3rd tab. then onPause calls but if change fragment from navigaton drawer then nothing is called. @Override public void onPause() is_open = false; Log.d( "receiversender: ", "on pause"); super.onPause();

            – Asif Ullah
            Nov 14 '18 at 6:21






          • 1





            That is not called because fragment is inside another parent fragment, so it is not destroyed. It just goes in stack.

            – Khemraj
            Nov 14 '18 at 6:29







          • 1





            Yes, check edit part of answer.

            – Khemraj
            Nov 14 '18 at 6:33
















          Broadcast works perfectly. but on pause is not called when i switch to other activity or fragment.

          – Asif Ullah
          Nov 14 '18 at 6:08





          Broadcast works perfectly. but on pause is not called when i switch to other activity or fragment.

          – Asif Ullah
          Nov 14 '18 at 6:08




          1




          1





          Fragment onPause() will always called when user is going to other Screen, either Activity or Fragment. Did you set logs in onPause() of fragment?

          – Khemraj
          Nov 14 '18 at 6:13





          Fragment onPause() will always called when user is going to other Screen, either Activity or Fragment. Did you set logs in onPause() of fragment?

          – Khemraj
          Nov 14 '18 at 6:13













          yes. i have override all methods like onPause, Onstop, OnDestroy etc.. I have multiple Fragments in MainActivity and in fragment i have view pager and set 3 tabs. if swipe to 3rd tab. then onPause calls but if change fragment from navigaton drawer then nothing is called. @Override public void onPause() is_open = false; Log.d( "receiversender: ", "on pause"); super.onPause();

          – Asif Ullah
          Nov 14 '18 at 6:21





          yes. i have override all methods like onPause, Onstop, OnDestroy etc.. I have multiple Fragments in MainActivity and in fragment i have view pager and set 3 tabs. if swipe to 3rd tab. then onPause calls but if change fragment from navigaton drawer then nothing is called. @Override public void onPause() is_open = false; Log.d( "receiversender: ", "on pause"); super.onPause();

          – Asif Ullah
          Nov 14 '18 at 6:21




          1




          1





          That is not called because fragment is inside another parent fragment, so it is not destroyed. It just goes in stack.

          – Khemraj
          Nov 14 '18 at 6:29






          That is not called because fragment is inside another parent fragment, so it is not destroyed. It just goes in stack.

          – Khemraj
          Nov 14 '18 at 6:29





          1




          1





          Yes, check edit part of answer.

          – Khemraj
          Nov 14 '18 at 6:33





          Yes, check edit part of answer.

          – Khemraj
          Nov 14 '18 at 6:33

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293571%2fupdate-recycler-view-while-receive-fcm-notification%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Museum of Modern and Contemporary Art of Trento and Rovereto

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully