1

Is there a way to determine if any of the activities are visible or not (the situation when the app is in the background). I'm trying to implement un-logging mechanism without sessions and I did handle the situations when the screen is off / locked, timeout, pressing the home button, but when another app is being launched and putting my app in the background, well there's the problem.

Or a way to check if an activity is being paused because there's an intent were called to open a new activity. This way, say I have three activities A, B, C and currently B is opened. So,

@Override
protected void onStop() {
  if (startActivityCalled) {
    // it continues to activity `C` or any else for that matter
  } else {
    // if screen were locked, for example
    // it goes to the activity `A` and finishes the current activity
  }
}
Katherine
  • 576
  • 1
  • 7
  • 19
  • [Use `ProcessLifecycleOwner`](https://developer.android.com/reference/android/arch/lifecycle/ProcessLifecycleOwner.html) from the Architecture Components. – CommonsWare Oct 08 '17 at 18:00
  • 4
    Possible duplicate of [Checking if an Android application is running in the background](https://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background) – BDL Oct 10 '17 at 12:39

2 Answers2

0

Well, when your application goes into the background, the function onPause() of your current Activity is called.

You can easily override it and place your logic there

@Override 
protected void onPause() {
    // Another Activity has started, user has closed the application 
    // or started another one...
}

When your Activity comes into the foreground again, then onResume() is triggered.

@Override
protected void onResume() {
     // the user re-opened your application, or came back from
     // another activity
}

Note that onResume() is also always called after onCreate().

EDIT:

If I understood correctly all you need to know it's if there is a visible Activity. Not which one...

In that case @Journey's answer below should suit your needs.


I was wondering though, from where your will be checking if your Actvities are visible or not. I guess you will be using a Service or some sort of background mechanism.

In that case, a pattern commonly used pattern is the LocalBroadcastManager.

I had written a related answer here though you can find many others on SO.

Basically, in each of your activities, you register a Receiver when your Activity is visible, and un-register it when it goes into the background.

From your Service you send an Intent, and the Activity that is visible, if any, will respond to it.

Create the Receiver in your Activities.

this.localBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Do what you have to do here if you receive data from the Service / Background Task
    }
}

Set up a Filter that will distinguish your signal from others:

public static final IntentFilter SIGNAL_FILTER = new IntentFilter("com.you.yourapp.MY_SIGNAL");

Remember to register the Receiver every time any of your Activities is visible.

@Override
protected void onResume() {
    // Listen if a Service send me some data 
   LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(this.localBroadcastReceiver, SIGNAL_FILTER);
}

When the Activity is closed stop listening to the signal:

@Override
protected void onPause() {
    // I'm going to the background, no need to listen to anything anymore...
    LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(this.localBroadcastReceiver);
}

When your Service, or Background Task wants to send data to the currently visible Activity, you can call

final Intent intent = new Intent();
intent.setAction(SomeActivity.SIGNAL_FILTER);
// put your data in intent
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

If an Activity is visible it will receive the Intent, otherwise nothing will happen.

payloc91
  • 3,724
  • 1
  • 17
  • 45
  • The activities (plural). Otherwise, it would trigger every time the activity is changed. And thanks for the lifecycle reference, though I well aware of it. – Katherine Oct 08 '17 at 22:20
  • @Katherine is that what you were looking for? Otherwise could you explain your question better – payloc91 Oct 09 '17 at 05:17
  • What if activities are from library and you are not aware how much activities there are and when will be displayed? – raV720 Dec 07 '22 at 11:22
  • hey @raV720 I answered this question 5 years ago and I never worked with Android again. I think you are better off by posting a new question on SO – payloc91 Dec 07 '22 at 11:46
0

try to check isDestroyed() before doing any operation if the activity is in the background.

Ankit Aman
  • 999
  • 6
  • 15