Updating Activity when it comes back to the Foreground after data has already been retrieved












2















  • I have implemented a Service that receives notifications sent from
    the server.

  • I'm currently using a broadcast receiver to send data. The Broadcast Listener is updating the Activity just fine. However, when data is sent when the Activity is not on "onResume" state, the data is not received.

  • I got interested in using RXjava because I believe it could resolve the issue but don't know where to start.


The problem:
When the activity is not on the foreground, the Activity is not updating.



Not in the foreground: Meaning that I have called unregisterReceiver to unregister in my onPause method.



My current user case: I'm using a Service to intercept notification sent from server using FCM



public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "[" + MyFirebaseMessagingService.class.getName() + "]=";

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
}

/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


Log.d(TAG, "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());

// Handle message within 10 seconds
handleNow();
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

// <-----------RX JAVA Observable Implementation----------->
// I would like to use Rx java to send data to my activity ONLY if the activity is in the foreground
// because data will be used to update the UI
}

@Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
}


I looked into RXjava and I find out that the library takes care of asynchronous operations, but what I'm curious about is:

if there is any functions within RXjava that will automatically update that data once the Activity is back on the foreground?



If not, what are the other alternatives?



Maybe this question has already been answered but I could not find a precise user case to what I'm trying to achieve in here. Also, I might be asking the question wrongly, but that just explains my confusion here










share|improve this question
























  • To update data once the activity back on the foreground you can register as a lifecycle observer. Look at LiveData source code you will get the solution.
    – Mohit Chauhan
    Nov 21 at 6:39








  • 1




    notificatin data is very small so you can save in shared prefs and later when the activity resumes display it form prefs
    – Har Kal
    Nov 21 at 22:59










  • I went ahead and used RxJava that does the job using the function onNext on the observable and observer.
    – Red M
    Nov 21 at 23:19










  • I agree with Har Kal. You can store this in a SharedPref and reference that once the Activity starts again. Very simple. This is what I am doing.
    – XO.
    Dec 10 at 19:41
















2















  • I have implemented a Service that receives notifications sent from
    the server.

  • I'm currently using a broadcast receiver to send data. The Broadcast Listener is updating the Activity just fine. However, when data is sent when the Activity is not on "onResume" state, the data is not received.

  • I got interested in using RXjava because I believe it could resolve the issue but don't know where to start.


The problem:
When the activity is not on the foreground, the Activity is not updating.



Not in the foreground: Meaning that I have called unregisterReceiver to unregister in my onPause method.



My current user case: I'm using a Service to intercept notification sent from server using FCM



public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "[" + MyFirebaseMessagingService.class.getName() + "]=";

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
}

/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


Log.d(TAG, "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());

// Handle message within 10 seconds
handleNow();
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

// <-----------RX JAVA Observable Implementation----------->
// I would like to use Rx java to send data to my activity ONLY if the activity is in the foreground
// because data will be used to update the UI
}

@Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
}


I looked into RXjava and I find out that the library takes care of asynchronous operations, but what I'm curious about is:

if there is any functions within RXjava that will automatically update that data once the Activity is back on the foreground?



If not, what are the other alternatives?



Maybe this question has already been answered but I could not find a precise user case to what I'm trying to achieve in here. Also, I might be asking the question wrongly, but that just explains my confusion here










share|improve this question
























  • To update data once the activity back on the foreground you can register as a lifecycle observer. Look at LiveData source code you will get the solution.
    – Mohit Chauhan
    Nov 21 at 6:39








  • 1




    notificatin data is very small so you can save in shared prefs and later when the activity resumes display it form prefs
    – Har Kal
    Nov 21 at 22:59










  • I went ahead and used RxJava that does the job using the function onNext on the observable and observer.
    – Red M
    Nov 21 at 23:19










  • I agree with Har Kal. You can store this in a SharedPref and reference that once the Activity starts again. Very simple. This is what I am doing.
    – XO.
    Dec 10 at 19:41














2












2








2


0






  • I have implemented a Service that receives notifications sent from
    the server.

  • I'm currently using a broadcast receiver to send data. The Broadcast Listener is updating the Activity just fine. However, when data is sent when the Activity is not on "onResume" state, the data is not received.

  • I got interested in using RXjava because I believe it could resolve the issue but don't know where to start.


The problem:
When the activity is not on the foreground, the Activity is not updating.



Not in the foreground: Meaning that I have called unregisterReceiver to unregister in my onPause method.



My current user case: I'm using a Service to intercept notification sent from server using FCM



public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "[" + MyFirebaseMessagingService.class.getName() + "]=";

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
}

/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


Log.d(TAG, "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());

// Handle message within 10 seconds
handleNow();
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

// <-----------RX JAVA Observable Implementation----------->
// I would like to use Rx java to send data to my activity ONLY if the activity is in the foreground
// because data will be used to update the UI
}

@Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
}


I looked into RXjava and I find out that the library takes care of asynchronous operations, but what I'm curious about is:

if there is any functions within RXjava that will automatically update that data once the Activity is back on the foreground?



If not, what are the other alternatives?



Maybe this question has already been answered but I could not find a precise user case to what I'm trying to achieve in here. Also, I might be asking the question wrongly, but that just explains my confusion here










share|improve this question
















  • I have implemented a Service that receives notifications sent from
    the server.

  • I'm currently using a broadcast receiver to send data. The Broadcast Listener is updating the Activity just fine. However, when data is sent when the Activity is not on "onResume" state, the data is not received.

  • I got interested in using RXjava because I believe it could resolve the issue but don't know where to start.


The problem:
When the activity is not on the foreground, the Activity is not updating.



Not in the foreground: Meaning that I have called unregisterReceiver to unregister in my onPause method.



My current user case: I'm using a Service to intercept notification sent from server using FCM



public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "[" + MyFirebaseMessagingService.class.getName() + "]=";

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
}

/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


Log.d(TAG, "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());

// Handle message within 10 seconds
handleNow();
}

// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}

// <-----------RX JAVA Observable Implementation----------->
// I would like to use Rx java to send data to my activity ONLY if the activity is in the foreground
// because data will be used to update the UI
}

@Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
}


I looked into RXjava and I find out that the library takes care of asynchronous operations, but what I'm curious about is:

if there is any functions within RXjava that will automatically update that data once the Activity is back on the foreground?



If not, what are the other alternatives?



Maybe this question has already been answered but I could not find a precise user case to what I'm trying to achieve in here. Also, I might be asking the question wrongly, but that just explains my confusion here







java android rx-java2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 18:12

























asked Nov 20 at 20:37









Red M

93631126




93631126












  • To update data once the activity back on the foreground you can register as a lifecycle observer. Look at LiveData source code you will get the solution.
    – Mohit Chauhan
    Nov 21 at 6:39








  • 1




    notificatin data is very small so you can save in shared prefs and later when the activity resumes display it form prefs
    – Har Kal
    Nov 21 at 22:59










  • I went ahead and used RxJava that does the job using the function onNext on the observable and observer.
    – Red M
    Nov 21 at 23:19










  • I agree with Har Kal. You can store this in a SharedPref and reference that once the Activity starts again. Very simple. This is what I am doing.
    – XO.
    Dec 10 at 19:41


















  • To update data once the activity back on the foreground you can register as a lifecycle observer. Look at LiveData source code you will get the solution.
    – Mohit Chauhan
    Nov 21 at 6:39








  • 1




    notificatin data is very small so you can save in shared prefs and later when the activity resumes display it form prefs
    – Har Kal
    Nov 21 at 22:59










  • I went ahead and used RxJava that does the job using the function onNext on the observable and observer.
    – Red M
    Nov 21 at 23:19










  • I agree with Har Kal. You can store this in a SharedPref and reference that once the Activity starts again. Very simple. This is what I am doing.
    – XO.
    Dec 10 at 19:41
















To update data once the activity back on the foreground you can register as a lifecycle observer. Look at LiveData source code you will get the solution.
– Mohit Chauhan
Nov 21 at 6:39






To update data once the activity back on the foreground you can register as a lifecycle observer. Look at LiveData source code you will get the solution.
– Mohit Chauhan
Nov 21 at 6:39






1




1




notificatin data is very small so you can save in shared prefs and later when the activity resumes display it form prefs
– Har Kal
Nov 21 at 22:59




notificatin data is very small so you can save in shared prefs and later when the activity resumes display it form prefs
– Har Kal
Nov 21 at 22:59












I went ahead and used RxJava that does the job using the function onNext on the observable and observer.
– Red M
Nov 21 at 23:19




I went ahead and used RxJava that does the job using the function onNext on the observable and observer.
– Red M
Nov 21 at 23:19












I agree with Har Kal. You can store this in a SharedPref and reference that once the Activity starts again. Very simple. This is what I am doing.
– XO.
Dec 10 at 19:41




I agree with Har Kal. You can store this in a SharedPref and reference that once the Activity starts again. Very simple. This is what I am doing.
– XO.
Dec 10 at 19:41

















active

oldest

votes











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%2f53401134%2fupdating-activity-when-it-comes-back-to-the-foreground-after-data-has-already-be%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53401134%2fupdating-activity-when-it-comes-back-to-the-foreground-after-data-has-already-be%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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'