How to password protect an application in Android - android

I want the user to enter a password everytime he tries to enter the application. That is, the user must enter the password everytime the app comes to foreground from background, be it by pressing the launcher icon or long-pressing the home key
I sort-of achieved the first part because the launcher intent is fired and i get a callback in onRestart of that activity.
But by long-pressing home key and launching the does not provide callback to onRestart.
Also what if the user launches the app by pressing the notification from, the notification bar. How do I distinguish whether the app was originally in background or fore-ground before the user clicked the notification

In your onResume call, set a loginCounter += 1, in your onPause -= 1.
if loginCounter == 0 => show Login Dialog.
In the Notification Bar you set an Intent to call the Activity, correct? Simply add a parameter "isCalledByNotificationBar" as boolean in there.

If onResume doesn't work, how about onWindowFocusChanged(boolean)?

Perhaps what you should do is on re-entry into the application see if it's been less than 30 (?) seconds since a timestamp that you update when any activity of your application was last paused. If it's been less, don't ask for a password. Thinking being that this short time window might smooth over some transitions you don't want to log out on.

Related

OnWindowFocusChanged not called when pressing back key from another task

Here is the sequences that triggers my "bug".
I launch my app. Then I receive a new snapchat/sms/anything new. I pull the notification bar from top of my phone and clic on that new snapchat which makes my phone launch the snapchat activity. If now I press the back key it goes back to my app BUT, OnWindowFocusChanged is not changed ! Why ? How do I handle this ?

Activity not closing properly

I am calling one activity on click of status bar notification which is having a Complete button. on click of btn. i have folllowing code -
public void completeTask(){
taskDBAdapter.deleteReminder(rowId);
taskDBAdapter.close();
Intent intent = new Intent(this, TaskManagerActivity.class);
startActivity(intent);
finish();
}
whhen i click complete btn new activity (TaskManagerActivity) gets opened properly.But if i reopen my application it still tries to open this activity and not my default landing activity. Any help on this.??
EDIT -
I have tried repositioning my finish() statement . Still its not working.
EDIT 1.1 -
Ok I will provide some details here. Assume my app has two activities
Main Activity
Notification Activity
My app create some notification to display on Status bar. So as soon as i click on status bar Notification actvty will open. Now there is a button called Complete on click of which the code given will fire and main activity (in the code TaskManagerActivity.class) will open. But after I press back button in my app and again reopen it , it opens the notification activity when it should have fired the main activity (as it is launching activity).
Thanks,
Ray
That's the default way android functions. If you press the home button and then open your app again, it will restore the apps previous state (unless it has killed the apps processes and activities due to memory constraints). So you are not actually restarting your app but only restoring it.
If you wanna quit the app, then press the back button. Now when you re-open the app, the original activity will be launched.
Do not modify this behavior. It is the default system behavior and users expect it to work this way. Your app is fine :-)
- First of all the behavior which you are experiencing is the way Android is made to function, moreover when a user gets a call while this app is open, after finishing the app he will definitely want to get back to the state where he left the application.
Still if you want it that way, here it is.....
- Make sure your application has only single instance of it running, by using android:launchMode="singleTask", or android:launchMode="singleInstance"
- Then finish() your Activity at onPause().
#Override
void onPause()
{
super.onPause();
finish();
}

Android auth-dialog when app starts

I have some questions. I'm trying to do auth-dialog, where user enters pass to work with app. Entered pass I store in SharedPreferences. So, the 1-st question: is this right idea from the security point of view to store password in that way ?
2nd and the main question: this dialog must be opened ONLY when app starts. Now it's done in onCreate() - method. But it's not right, because, for example: I run app, enter pass, clicked Ok-button and dialog closed. But when I change the rotation of screen, this dialog opens again, because when we rotate screen, Activity destroyed and onCreate()-method called again. So, where should I place code that opens my auth dialog, to open it only when app starts ?
Thanks for all answers !

how to check whether the activity was launched from notification or from the user click on home screen dashboard app icon in android

how to check whether the activity was launched from notification or from the user click on home screen dashboard app icon in android. Is there any parameters that tells that this activity was launched due to user clicked on notification..
There already were several questions on this topic:
detecting application launch from
home or history
Determine if app was launched from
home screen?
As of the home screen part, as far as I know there is no way to detect that.
However, you can detect whether your activity is launched from the notification icon:
When setting up your notification, you put an extra into it's PendingIntent, say fromNotification: boolean.
In your main activity's onCreate method you check the intent's extras, and if (there are any and) the fromNotification is among them with the value true, than you know it has been invoked by clikcing on the notification icon.
You can use startActivityForResult() when launching Activity from your App and then check if getCallingActivity() returns null. If it does, your activity has been launched from notification.

Determine when application icon is clicked to launch the app in android

Is there any way in android to determine when the user clicked the app icon to launch the app ? I mean say a user was using my app. Then he presses the home key as a result of which the app goes to the background. After sometime he clicks the app icon again. My question is do I get a call-back for this ?
Just to inform, I used the flag android:clearTaskOnLaunch="true" in my launcher activity. As a result, its onResume method was called and I could identify that the launcher icon was clicked
It will call the onResume() method if the app is already in the stack. And if the app not in the stack then it will call the onCreate() method.
This mechanism is based on the launchMode specified for the activity.
please read http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
How many activities your application has, you will get a callback onResume() for the last open activity.
If an app comes from the background, you can check it by getting intent flags
intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0
it will be true if your app comes from the background. If you click on the app icon to open it, the above logic will not be true.

Categories

Resources