1

I am developing an application which request user to enter passcode whenever he/she leave the application or Press home button. This will be applied on all the application activities. i have tried the Activity lifecycle onResume,onRestart and so on. but those will be called when moving from one activity to another activity. i have tried also this code

@Override
public void onUserLeaveHint() {
    long uiDelta = (System.currentTimeMillis() - userInteractionTime);

    super.onUserLeaveHint();
    Log.i("bThere","Last User Interaction = "+uiLag);
    if (uiDelta < 100)
        Log.i("appname","Home Key Pressed");    
    else
        Log.i("appname","We are leaving, but will probably be back shortly!");  

But, this will be called when switching between activities not only when pressing home button.

Any idea on how I can detect leaving the application ?

Abdqr92
  • 13
  • 3
  • try using onWindowFocusChanged and onStop methods . more can be found here -> http://vardhan-justlikethat.blogspot.in/2013/05/android-solution-to-detect-when-android.html – shaydel Oct 01 '15 at 08:29
  • @shaydel I tried it but it is the same. when i move to another activity by Intent it will give me that the application is in the background. – Abdqr92 Oct 01 '15 at 08:39
  • i must not understood you, you seem to find that your app is on the background , when another app is in front , what is missing? – shaydel Oct 01 '15 at 08:45
  • try `HomeWatcher` as @Jack suggests in the following post -> http://stackoverflow.com/a/27956263/1119365 – shaydel Oct 01 '15 at 08:48

6 Answers6

0

You can check if your app is in background or not

1 - you should include android.permission.GET_TASKS in your AndroidManifest.xml

2 -Put this code in you Application class.


public static boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
      ComponentName topActivity = tasks.get(0).topActivity;
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
        return true;
      }
    }

    return false;
  }
Hoshouns
  • 2,420
  • 23
  • 24
0

You can send an extra boolean on intents that execute the flow between your activities indicating that the passcode request should not be requested. then on your activities onResume you consume this boolean once and flip it to false, so that next time the activitry is resumed the passcode will be requested again. this should work i think. please let us know if it did :)

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
0

Delete the history when the User switches between Activities. So He Cant use the backbutton.

Patricia
  • 2,885
  • 2
  • 26
  • 32
0

I also have similar issue when i want to disconnect chat server connection when the app is no longer used or in background mode. In iOS there is a method

 didEnterInBackground()

which is used to detect when the app is in background or not. I was looking for similar behaviour in android but ended up using workaround to solve .

So what did solve the solution to my prob is , i add timer on every activity's onPause and onResume . Start timer on host activity's onPause and stop on going timer on target activity's onResume. So if there is no target activity ( app in background or another app come in ) , it would automatically disconnect the chat server.

  timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            disconnectChatServer();
        }
    },1000);

You can adjust delay which in millisecond .

Tixeon
  • 930
  • 1
  • 12
  • 27
0
/**
* 
* @param context
* @return 
*/
public static boolean isBackground(Context context) {    
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    for (RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.processName.equals(context.getPackageName())) {
            if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                Log.i(String.format("Background App:", appProcess.processName));
                return true;
             }else{
                Log.i(String.format("Foreground App:", appProcess.processName));
                return false;
             }
        }
    }
    return false;
}
starkshang
  • 8,228
  • 6
  • 41
  • 52
0

While leaving activity or Home button clicked,

Activity`s lifecycle method, onPause() method used to call. If application need memory for other operation then moved to onStop() method.

Kindly let me know if i have cleared your question.

Thanks.

Jayaprakash
  • 101
  • 5