2

I would like to know when my app is back from background. onResume() is not a good solution, because i have another activities beside the Main Activity, so it can back from background to each of them. The purpose is to use Google analytics and to know when a user is launching the app and also bring it back from the background.

Thank you all and much appreciation.

Simon
  • 14,407
  • 8
  • 46
  • 61
Sagi Shchori
  • 150
  • 9
  • possible duplicate of [How to detect when an Android app goes to the background and come back to the foreground](http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo) – Simon Dec 22 '14 at 14:25
  • [modern answer on another thread: Use ProcessLifecycleOwner](https://stackoverflow.com/a/48767617/199364) – ToolmakerSteve Mar 24 '19 at 21:37

3 Answers3

2

Create a common base class which extends Activity. Implement onResume() with the functionality you need. Then extend all of your other activities from this base class.

The onResume() in the base class should call super.onResume() and this should also be the first line in each of the individual activity onResume methods.

Base class

public class BaseActivity extends Activity

@Override
public void onResume(){
    super.onResume();
    // code to do your analytics stuff
}

Derived Activities

public class MainActivity extends BaseActivity

@Override
public void onResume(){
    super.onResume();
    // code for the individual activity
}
Simon
  • 14,407
  • 8
  • 46
  • 61
  • But would it will be called each time any of those activities call to onResume() (for example moving between activities)? in that case I will get false data for using the app. – Sagi Shchori Dec 22 '14 at 14:34
  • Yes it will, but read the thread I linked in the "duplicate". – Simon Dec 22 '14 at 14:44
  • Doesn't help much, even with flags it causes problems but thanks anyway for your time. – Sagi Shchori Dec 23 '14 at 15:05
0

You can detect background application with ActivityManager.getRunningAppProcesses() which returns a list of RunningAppProcessInfo records. If your application is in the background check RunningAppProcessInfo.importance field equals to RunningAppProcessInfo.IMPORTANCE_BACKGROUND while RunningAppProcessInfo.processName is equals to your application package name.

More info:

http://developer.android.com/intl/es/reference/android/app/ActivityManager.RunningAppProcessInfo.html

Mou
  • 2,027
  • 1
  • 18
  • 29
0

The android.arch.lifecycle package provides an interface that let you know when the app is back from background.

Your application should implement the LifecycleObserver interface:

public class MyApplication extends Application implements LifecycleObserver {

    @Override
    public void onCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    private void onAppBackgrounded() {
        Log.d("MyApp", "App in background");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private void onAppForegrounded() {
        Log.d("MyApp", "App in foreground");
    }
}

To do that, you need to add this dependency to your build.gradle file:

dependencies {
    implementation "android.arch.lifecycle:extensions:1.1.1"
}

As recommended by Google, you should minimize the code executed in the lifecycle methods of activities:

A common pattern is to implement the actions of the dependent components in the lifecycle methods of activities and fragments. However, this pattern leads to a poor organization of the code and to the proliferation of errors. By using lifecycle-aware components, you can move the code of dependent components out of the lifecycle methods and into the components themselves.

You can read more here: https://developer.android.com/topic/libraries/architecture/lifecycle

matdev
  • 4,115
  • 6
  • 35
  • 56