Using onNewIntent and activity lifecycle

print
https://stackoverflow.com/questions/27513909/using-onnewintent-and-activity-lifecycle-to-verify-if-a-message-has-been-seen

 

I have a push notification server to deliver messages to my Android app and I’d like to know the best way to notify the server when the message has been seen.

Currently i’m trying to send the ack message to the server as soon as the main activity of my app calls its onResume callback, but as I use a SingleTop main activity, and I treat some intents with the onNewIntent method, I cannot be sure if the onResume is due to an Intent or the app coming to the foreground. I’ve mapped the callback flow in the three situations my activity can be when it receives an intent to start it, and they are:

  • The activity is running on the foreground (top of the stack)(1)Running -> onPause() -> onNewIntent(intent) -> onResume()
  • The activity is paused on the foreground (screen of for example)(2)Paused -> onRestart() -> onStart() -> onResume() -> onPause() -> onNewIntent(intent)
  • The activity is paused on the background (i’m in another activity, for example)(3)Paused on background -> onNewIntent(intent) -> onRestart() -> onStart() -> onResume()

In the case i navigate to my activity without receiving an intent:

  • The activity is paused and the user navigates to it (4)Paused -> onRestart() -> onStart() -> onResume()

In cases 1, 3 and 4 the user ends in a screen where he/she can actually see the message, so i want to send the ack message.

If there was a callback that is called just in the case the activity really become visible in the screen it’d be perfect, but I’m having trouble finding it.

 

In activity the first method is onCreate. onCreate of activity add the fragment and in this moment onAttach is called. like in the pictureenter image description here

Ref: http://baiduhix.blogspot.com.br/2015/08/android-how-to-do-findviewbyid-in.html

EDIT:

updated lifecycle of Android

enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.