I am developing an Android application. I want to quit my application but application should be in running state in background. Whenever I click on application application should start from last quit point. I dont want to login again.
The same thing is happening when I press home button. But I want to implement similar functionality like Home button on my own button event. How should I proceed with that??
Though I have finished all other activities, still I need to login again. When I finish the activity my session ends there. And on next app start with login screen.
Whereas in case of home button click, it keeps my session and on next app start my app check onResume() event where I am checking whether session exist or not. If session is there I can enter directly into my account.
So Anybody have any idea what exactly android does when we press home button.
The best way I know is something like in your scenario where you want to quit:
Save the login credentials on login and clear on log out.
When your app launches check for credentials and if it is there then you should login without asking user.
The above process will not bring your last activity but the activity which you show after login.
To bring back the last activity where you quit, you can use moveTasktoback(true) as it will put your app in the background the same when you press Home key but it's behaviour will not remain constant as android can kill your app whenever it will require the memory.
You mean login to a web app? WebView saves cookie information application-wide regardless of activity.
Otherwise use a static variable or singleton class to hold any session state and on activity start, check the static variable or singleton class for any state else redirect them to the login screen.
You can call moveTaskToBack on your Activity.
create a button where you want to pause(i mean in which activity) the application. And write the code like below. This will pause your application.
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
moveTaskToBack(true);
}
});
Related
I'm working on an Android app that will show college fitness professors how their students are doing in their classes. Since this data is fairly sensitive (biometrics are shown, including weight, something many college students are self-conscious about) I don't want the data to be available to anyone who picks up the tablet. While I have a proper login screen created, complete with authentication for the database, etc. I have an issue when the home button is pressed. Since Android doesn't close a program immediately on leaving the app, it's possible to reopen it and return to where you were. I would like to force the app to return to the login screen each time (I've altered onBackPressed so you can't just return to the previous view from the login screen) so that you have to re-enter your credentials to get back into the app. However, I can't seem to do this. An answer I found on here said to use the following line:
android:clearTaskOnLaunch="true"
However, no matter what XML file I put it in, be it the Manifest or the individual Activity XMLs, it appears to do nothing. So, how do I ensure the login screen comes up each time the app is launched, regardless of whether it is starting from scratch or not?
Try to play around with onUserLeaveHint() method. If you read its documentation, it says:
Its Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called
So, when ever you detected home button pressed, you can finish the running activity/activities. So next time user click the app, it will start from the first login screen.
Hope this helps.
You should override onUserLeaveHint()
#Override
protected void onUserLeaveHint() {
// do your logic
//finish() or start login activity
}
You could set a flag when onPause() is initiated within the activity. And then when you return you could check the flag from within onResume() and then request a login from that point. This will be sure to request it each time; in a simple case of course.
Edit:
With multiple activities, you could check against a saved context to see if they are the same when you start a new activity. If the contexts differ then you can discard the context previous activities context and start a new activity.
If they are the same, then you have come back to the activity from itself (you have lowered and brought the screen back). You would have to use some form of saved state such as that to do it in this manner with multiple activities when outside the case of a simple application.
I found out how to do it in my case. For any others with the same problem, try following the example here:
Android detecting if an application entered the background
Question 1) I have 2 basic user Activity who has different TAB layout.
user1:Admin
user2:Consumer
whenever I started the app, by default It redirects me to Consumer Activity'sTab with no session. Then proceed to login only redirects to either Consumer Activity's Tab or Admin Activity's Tab according to login session.
when I logged in to Admin, I was redirected to Admin Activity's Tab with Admin session. (Correct)
but when I pressed back button,
instead of redirecting to Login Layout or close the application, It redirects to Consumer Tab with Admin session (Incorrect)
how should I Fixed this? I saw some sort of FLAG? I don know how to use it. If it is needed to use xxx_FLAG, which layout should I placed the FLAG?
Question 2) how to destroy the session whenever user close the app by pressing back button or home button on the phone?
my current situation is when I logged in as a Member, for example "abc". when i close the app and come in again, it remains "abc" session. Unless I logged out before closing the app then it works with no more session.
If it works for you, you may be able to get by with just keeping tight control on the backstack so you can't go back to the consumer tab once you login as an Admin.
But for your second question, overiding onBackPressed() and calling finish should achieve the result you want (whether that's recommended or not is not my place to debate)
#Override
public void onBackPressed() {
finish();
}
and as for the home button, that can't be overriden so I guess you would want to add finish() to various places that get called when the activity changes states (see the activity life cycle http://developer.android.com/reference/android/app/Activity.html)
Once a user signs out (from the preferences), the handler signs the user out and them takes them to the initial launch page.
The only problem is the user can hit the 'back' button which takes them to the other activities where they can see the info from the logged in person. What i want to do is make it work like someone has just opened the app for the first time when they have signed out.
I've tried to send the intent of starting the activity with 'FLAG_ACTIVITY_CLEAR_TOP' and 'SINGLE_TOP' flags but it doesn't seem to work.
Thanks
Try the FLAG_ACTIVITY_CLEAR_TASK option.
EDIT: just noticed that this is only in API 11. You'll probably need to do something like registering a BroadcastReceiver in all your Activities, then on the logout screen send an Intent that the BroadcastReceiver will catch. The Activity can then cleanly exit.
There are two things you can do, the first of which you've tried but isn't working for some reason.
One is to clear the back button stack when you launch your activity like this: myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); though I'm not sure why that's not working for you.
The other thing you can do is override the back button handling in your login activity so that it doesn't do anything. Here is example code to accomplish that:
#Override
public void onBackPressed()
{
return;
}
I have an issue where if my user is on the Dashboard screen and presses the phones 'Back' button, this will then return them to the login screen (which still has their details input) and if they login again, some variables are global so these are then effectively reused which effects the functionality of the application.
My thinking was I could override the onResume method when this activity is resumed and then clear everything but I am unsure on how to code this and clear the form and any variables still existing in the applications memory.
Thanks.
finish() your login screen when you logged in.
For example:
//I'm logged in, starting dashboard view
startActivity(intent);
//finishing login activity - I don't need it on back stack
finish();
The safest option would be to call finish() in the login activity after switching to the new activity. This will prevent the activity to go back to the login screen after pressing the back button, as that will remove that activity until it is manually started again.
I am writing an Android app which requires the user to be logged in. I have a main Activity, and I want that when the app launches, that it takes the user to a login screen if they are not already logged in.
How should I model this? Is the login page another Activity? I'm thinking not because I don't want the user to be able to get back there using the back button. Or is there a way to replace an activity with another so the back button problem doesn't happen?
Or, should I have just one activity and load in a login view and swap it out once the user logs in?
Are there any best practices around this?
Thanks!
I would check on the main or splash screen to check if the user is logged in, if not start the login activity.
Once the login completes, in the login activity, call this.finish()
If you need to change activities, you can call .finish() followed by starting whatever activity you wanted.
If getting back to the splash/main screen without being logged in is a problem, you can do the same thing there.
For this ask "I'm thinking not because I don't want the user to be able to get back there using the back button"
The answer is using the attribute noHistory in your Login Activity, like the example:
<activity
android:name="com.test.LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:noHistory="true"...
I hope it can help.
Yes, the login screen is an another activity. The back key problem is solved with onKeyDown() method of that activity. Read more here.
The whole flow can be imagined like this:
You're starting your app. The main activities check for login flag(in shared preferences or somewhere else). If user has logged in, then the main activity stays on the screen. If not, activity starts login activity using intent. Login activity performs the logging in and sets the login flag and start the main activity again.
Have you considered using a dialog for the login? I suppose what you could do is have the first Activity check at onCreate() using SharedPreferences if the user has already logged in, and if she hasn't then it generates the dialog. After logging in the dialog would change a variable which would be passed to SharedPreferences so that the user won't have to relogin if the screen orientation changes or the app pauses.