3

I have 2 activities which are using the same Android task - i.e. they will use the same back stack.

Let's call A the first activity in the back stack, and B the second activity in the back stack.

case 1: A is notified that it goes in the background when A was in the foreground and then B comes in the foreground (for any reason, like because it received an external intent).

case 2: A is notified that it goes in the background when A was in the foreground but another application was started in the foreground.

Question: How does my app differentiate between those 2 cases? Is there a way to know if the transition A in foreground -> A in background keeps the current task displayed on the screen?

Basically, I need to get an event when the Android task becomes visible or invisible.

Vincent Cantin
  • 16,192
  • 2
  • 35
  • 57
  • Why do you want to differentiate between that? Wouldn't it be enough for `B` to know that it should take over the back stack when it's called? – Terry Apr 24 '14 at 07:37
  • I want to avoid `A` to do something if I know that we are in the case 1. Basically, what I want to track is the visibility of the Android task. – Vincent Cantin Apr 24 '14 at 07:57
  • 1
    The OP wants to know when the app's activity is going to background due to another activity (from the same app) or another app coming into foreground. – David Andreoletti Apr 24 '14 at 08:22

4 Answers4

3

If I understand you correctly, your Activity A would do something different if Activity B doesn't get activated after A was paused.

First of all, it's not a good idea to let an activity do something when it's not visible anymore. As we see in the Android lifecycle, it could get stopped: enter image description here

I would put your background code into a service. This service gets notified whenever Activity A or B gets paused or resumed. There you could add a little timeout which has to pass till you start your logic.

If you do that, that would happen:

Case 1: Activity A goes into background and sends a message in the onPause handler to your service. Service starts its timeout timer. Activity B gets started and sends a message in it's onResume handler to the service.

Now the services knows it's still the same activity and acts accordingly.

Case 2: Activity A goes into background and sends the message. The service starts its timeout timer. It timeouts (I think 200ms would be enough) and now your service knows the task isn't active anymore and acts accordingly.

schlingel
  • 8,560
  • 7
  • 34
  • 62
  • Thank you for your answer which I +1ed. This solution is exactly what we implemented at the moment. The problem with this approach is that we know what happens only after a timeout delay. I was wondering if there was a better way of doing it. – Vincent Cantin Apr 24 '14 at 10:19
0

Use log or Toast in activity life cycle to display message

user3563954
  • 37
  • 1
  • 13
  • My question is not *how the user knows*, it is *how the program knows*. Sorry for the confusion. – Vincent Cantin Apr 24 '14 at 07:57
  • so use log or system.out.println it display in logcat – user3563954 Apr 24 '14 at 08:07
  • I am sorry, I don't see how this can solve the problem. – Vincent Cantin Apr 24 '14 at 08:18
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println("Created"); } @Override protected void onPause() { super.onPause(); System.out.println("Pause"); } protected void onResume() { super.onResume(); notify("onResume"); } protected void onStop() { super.onStop(); System.out.println("Stop"); } protected void onDestroy() { super.onDestroy(); System.out.println("Destroyed"); } – user3563954 Apr 24 '14 at 08:32
  • 1
    @user3563954 It doesn't look like you understood the question. It's about how an activity knows if the same task is active after it was suspended or not. – schlingel Apr 24 '14 at 09:35
0

Keep a static flag in Activity B. set it on resume and unset it on pause.

Tech Agent
  • 657
  • 5
  • 12
0

Override onPostResume

@Override
protected void onPostResume() {

    //Your code here

    super.onPostResume();
}
Tareq
  • 689
  • 1
  • 10
  • 22