5

I am new to android and I need to know whenever App resumes from background I need to display a pass code lock screen I followed this link and able to get it but whenever I use to invoke camera intent or gallery pick intent App is going to background and lock screen is appearing instead I need to know whether App reached background on user home button press

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Software Sainath
  • 1,040
  • 2
  • 14
  • 39

5 Answers5

2

Whitelist the "good" actions

I suggest you try thinking about it the other way around. For the actions you want the user to take (like launch the camera, or pick from the gallery) you can white-list that action.

This way, if any other reason causes the app to go into the background (like the user getting a phone call) you can show the lock screen when they return.

When you start the camera or gallery activities you can define a custom requestCode and check for this when they return to your app via onActivityResult

Something like this:

private static final String MIME_TYPE = "image/*";
private static final int MY_GALLERY_REQUEST_CODE = 90210;

private void launchGallery()
{
    Intent i= new Intent();
    i.setType(MIME_TYPE);
    i.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(i, MY_GALLERY_REQUEST_CODE);
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{   
    if (requestCode == MY_GALLERY_REQUEST_CODE) 
    {  
        //user is returning from the gallery picker
    } 
    else  
    {
        showLockScreenIfNeeded();
    }
}

@Override
protected void onResume()
{
    showLockScreenIfNeeded();
}

Also from: This other SO answer: onActivityResult() & onResume()

onActivityResult() should be called before onResume()

From the docs:

protected void onActivityResult (int requestCode, int resultCode, Intent data)

Since: API Level 1 Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation. You will receive this call immediately before onResume() when your activity is re-starting.

Community
  • 1
  • 1
pjco
  • 3,826
  • 25
  • 25
  • 2
    I think this is a good idea but needs work - Coming back to the app after pressing home does not call onActivityResult (but it does call onResume) – Greg Ennis Jan 09 '14 at 02:37
  • 2
    be careful because onActivityResult and onResume are both called when returning from gallery, and in my experience the order is not always the same – Greg Ennis Jan 09 '14 at 02:41
  • 1
    This is a good point, I would probably make it so showMyLockScreen contain logic to handle some of that -- updated answer, thanks again for the tips. – pjco Jan 09 '14 at 02:45
  • 2
    I think this is the only possible solution, which will cover most of the @Software Sainath requirements. It won't actually meet the requirement to show lock screen **always** when restoring from background: if the app is sent to background at third-party Gallery activity, it'll be restored at the same point, not at lock screen. However, I don't think it's in fact possible to handle this particular case. – a.ch. Jan 09 '14 at 13:51
  • In fact, it's possible, see the [answer](http://stackoverflow.com/a/21087910/1037294) – a.ch. Jan 15 '14 at 08:05
1

You can use following method to check it

@Override
protected void onUserLeaveHint() 
{
   super.onUserLeaveHint();
   // Put your code here
}
Vigbyor
  • 2,568
  • 4
  • 21
  • 35
  • 1
    This unfortunately won't handle the case when another app is brought to front of our app (I suppose, @Software Sainath's intention is to automatically log-out in any case of app going to background). – a.ch. Jan 08 '14 at 15:33
1

onResume() (or onStart()) cannot be used for your purpose since the callback is invoked whenever the activity becomes visible.

Instead of it, you can override onNewIntent(Intent) and then check whether Intent.FLAG_BROUGHT_TO_FRONT is set on the flag inside the intent.

That flag is automatically set by the Android system when the task where your activity is brought from background to front (usually by pressing the app icon of an already-running app). This is NOT set when your activity is returned from another activity such as camera or gallery for attaching pictures. Note that in order to use the flag, you should set the launch mode of the activity to 'singleTask'. For details please refer to http://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_launchMode.

Jiyong Park
  • 664
  • 3
  • 6
1

Check the below code continuously that means u can use Timer class.

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<RunningTaskInfo> runningTasks = null;
    try {
        runningTasks = activityManager.getRunningTasks(1);
    } catch (Exception e) {

    }
    RunningTaskInfo runningTaskInfo = runningTasks.get(0);
    ComponentName topActivity = runningTaskInfo.topActivity;
if(topActivity.getPackageName().equals(your packagename)){
       S.o.p("fine");}
else{
       S.o.p.("application sent to background");}
user543
  • 3,623
  • 2
  • 16
  • 14
  • There are [issues](http://stackoverflow.com/questions/3667022/android-is-application-running-in-background/5862048#5862048) with the solution. – a.ch. Jan 13 '14 at 09:29
1

I've found the way to overcome the issue I described with the @pjco solution.

To make it possible to show lock screen even when brought to background from third-party Activities, simply use FLAG_ACTIVITY_NO_HISTORY when starting these Activities:

Intent intent = new Intent(Intent.ACTION_PICK, Images.Media.INTERNAL_CONTENT_URI);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, REQCODE_PICK_GALLERY);
Community
  • 1
  • 1
a.ch.
  • 8,285
  • 5
  • 40
  • 53