How to Detect When an Android App Goes to the Background and Come Back to the Foreground

How to detect when an Android app goes to the background and come back to the foreground

The onPause() and onResume() methods are called when the application is brought to the background and into the foreground again. However, they are also called when the application is started for the first time and before it is killed. You can read more in Activity.

There isn't any direct approach to get the application status while in the background or foreground, but even I have faced this issue and found the solution with onWindowFocusChanged and onStop.

For more details check here Android: Solution to detect when an Android app goes to the background and come back to the foreground without getRunningTasks or getRunningAppProcesses.

How to detect when an Android app goes to the background and come back to the foreground

The onPause() and onResume() methods are called when the application is brought to the background and into the foreground again. However, they are also called when the application is started for the first time and before it is killed. You can read more in Activity.

There isn't any direct approach to get the application status while in the background or foreground, but even I have faced this issue and found the solution with onWindowFocusChanged and onStop.

For more details check here Android: Solution to detect when an Android app goes to the background and come back to the foreground without getRunningTasks or getRunningAppProcesses.

How to detect whenever app comes to foreground in android

Now you can add a LifecycleObserver to your app when it's created to detect when your app goes to foreground/background.

class MyApp : Application() {

private lateinit var appLifecycleObserver : AppLifecycleObserver

override fun onCreate() {
super.onCreate()
appLifecycleObserver = AppLifecycleObserver()
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleObserver)
}
}


class AppLifecycleObserver() : LifecycleObserver {

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onEnterForeground() {
// App entered foreground
// request passpharse
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onEnterBackground() {
// App entered background
}

}

Check if app's in foreground or background

Try this

your build.gradle file:

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

Then in your Application class,add :

class ArchLifecycleApp : Application(), LifecycleObserver {

override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}

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

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

update your AndroidManifest.xml file :

<application
android:name=".ArchLifecycleApp"
//Your extra code
....>
</application>

Callback when task goes into background or comes into foreground?

Using the CustomTabsCallback you can listen when the Tab becomes hidden (goes into background) using the TAB_HIDDEN callback or TAB_SHOWN callback when the Tab becomes visible (goes into foreground).

From the Documentation:

TAB_HIDDEN

Sent when the tab becomes hidden.

TAB_SHOWN

Sent when the tab becomes visible.

Below is a full working example of how you can use the above callbacks:

public class CustomTabsActivity extends AppCompatActivity {

private CustomTabsServiceConnection mCustomTabsServiceConnection;
private CustomTabsClient mCustomTabsClient;
private CustomTabsSession mCustomTabsSession;
private CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.customTabsButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomTabs();
}
});
initCustomTabs();
}

@Override
protected void onStart() {
super.onStart();
CustomTabsClient.bindCustomTabsService(this, "com.android.chrome", mCustomTabsServiceConnection);
}

@Override
protected void onResume() {
super.onResume();
}

@Override
protected void onStop() {
super.onStop();
}

@Override
protected void onDestroy() {
super.onDestroy();
}

private void initCustomTabs() {
mCustomTabsServiceConnection = new CustomTabsServiceConnection()
{
@Override
public void onCustomTabsServiceConnected(@NotNull ComponentName componentName, @NotNull CustomTabsClient customTabsClient)
{
mCustomTabsClient = customTabsClient;
mCustomTabsClient.warmup(0L);
mCustomTabsSession = mCustomTabsClient.newSession(new CustomTabsCallback()
{
@Override
public void onNavigationEvent(int navigationEvent, Bundle extras) {
switch (navigationEvent)
{
case CustomTabsCallback.TAB_SHOWN:
//Sent when the tab becomes visible (goes into foreground)
break;
case CustomTabsCallback.TAB_HIDDEN:
//Sent when the tab becomes hidden (goes into background)
break;
}
}
});
builder.setSession(mCustomTabsSession);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mCustomTabsClient = null;
}
};
CustomTabsClient.bindCustomTabsService(this, "com.android.chrome", mCustomTabsServiceConnection);
}

private void showCustomTabs(){
builder.setShowTitle(true);
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse("https://stackoverflow.com/"));
}
}


Related Topics



Leave a reply



Submit