Android auth-dialog when app starts - android

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 !

Related

Start Android App on same activity every time

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

Need to restore the same view in android application on clicking the app icon

I am new to android development. I am developing an android app, in which I have few views, say login view, view A, view B, view C and view D etc..
My initial view is a login view. User enter their credentials and if they are valid, view A (for a particular user role) will be displayed.
Now, user clicks the phone home button and now my application is running in background mode.
When user clicks back my app icon, it displays the login view. But actually I need to display view A.
Why is it so? I don't find any error stack trace. Can anyone please suggest me what I am doing wrong.
Thank you.
When the application is brought back up, onCreate is called again. If you want to save your state, you should use SharedPreferences and save them in onPause. Then recall them in onResume.
There are a couple of things you can do. Normally you would override onSaveInstanceState(Bundle savedInstanceState), see for example
Saving Android Activity state using Save Instance State
But of course in your case you must be careful with how long the login state should be valid etc.

Pause your application

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);
}
});

How to check which activity finished

In my Android application I need to Override the onResume() method to check which of two possible activities just finished. The user will either have entered an amount of money, or named and chosen a percent for a category. How can I do that? Also, if a user presses home and then goes back to my app, is onResume() called? If so, I can just call super.onResume(), right?
I have three classes: PaySaver, NewSavingCategory, and NewPaycheck. PaySaver.java is the main Activity, and there are two buttons: New Paycheck (launches a dialog box where a user inputs $ (NewPaycheck.java)) and New Saving Category (launches a dialogbox where a user inputs a name and a % (NewSavingCategory.java)). When the dialog box is closed via an enter button, I want the main activity to be updated with the information entered.
Thanks!
How can I do that?
Most likely, you don't. Both of those other activities updated your central data model. In onResume(), you update your UI from that same central data model. Hence, it does not matter where the user came from -- you are grabbing the latest data.
Also, if a user presses home and then goes back to my app, is onResume() called?
On the activity they return to, yes.
If so, I can just call super.onResume(), right?
Not only "can" you do that, you have to call super.onResume(), or your activity will crash.

How would one design a flow where one of the screens is a login screen?

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.

Categories

Resources