Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect application foreground event [duplicate]

Tags:

android

Does anybody know some event listener for detecting when the application comes back to the foreground from the background? I searched a lot, but I didn't find such a listener. Any other solution is appreciated. Thank you!

like image 888
strongmayer Avatar asked Mar 22 '12 13:03

strongmayer


People also ask

How to detect app running from background to foreground in Salesforce?

Here is achieved by lifecycle to detect app running from background to foreground. For Forms, you can detect the app from background to foreground by OnResume()method in the App.xaml.cs. If you want to detect the app from background to foreground in different platform. For Android: Open the MainActivity.cs, add the OnResumemethod.

How to detect app is in background or foreground in React Native?

The AppState component in react native is used to notify the user whether the app is in background or foreground and returns a string. So in this tutorial we would Detect App is in Background or Foreground using AppState in react native android iOS application.

Why can’t I detect when a user backgrounds and foregrounds my App?

It’s simply because there is nothing obvious built into the Android SDK enabling developers to hook into application lifecycle events. Activities and Fragments have all the callbacks under the sun to detect lifecycle change, but there is nothing for the whole application. So how do you detect when a user backgrounds and foregrounds your app.

How do I detect background events in ontrimmemory?

We outlined earlier that we could use onTrimMemory and the TRIM_MEMORY_UI_HIDDEN flag to detect background events. So lets do that now. Add this into the onTrimMemory method callback body So now we have the background event covered lets handle the foreground event. To do this we are going to use the onActivityResumed.


2 Answers

You may need to be more specific about your use case.

Check out the Activity Lifecycle.

Both onResume(), and onStart() will get called when your activity comes to the foreground.

Edit: onRestart()?

like image 144
FoamyGuy Avatar answered Sep 28 '22 15:09

FoamyGuy


I does something like this, in a BaseActivity

in onResume I check enterCount is equals 0 and then add the enterCount in onPause I delay the enterCount to decrease in 300ms, and it seems good when the onCreate isn't delay two long, or we can do something in onCreate to avoid the first time.

protected void onResume(){
         int  enterCount=GlobalManager.getInstance().getEnterCount();
        if(enterCount==0){
            //do some thing for first enter 
        }
        GlobalManager.getInstance().increaseEnterCount();
}

protected void onPause(){
        GlobalManager.getInstance().decreaseEnterCountDelay();

}
like image 43
powerfj Avatar answered Sep 28 '22 16:09

powerfj