Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android check whether app reached background when user pressed Home Button?

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

like image 917
Software Sainath Avatar asked Nov 29 '13 11:11

Software Sainath


People also ask

How can I tell if my home button is pressed Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken a text view.

What happens to activity when home button is pressed?

When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity. Eventually onDestroy method will be raised to confirm that your activity is finished.

Does Pressing Home button close App?

Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background.


2 Answers

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.

like image 119
pjco Avatar answered Sep 22 '22 04:09

pjco


You can use following method to check it

@Override
protected void onUserLeaveHint() 
{
   super.onUserLeaveHint();
   // Put your code here
}
like image 34
Vigbyor Avatar answered Sep 24 '22 04:09

Vigbyor