I have an application that starts with a login screen and the goes to a gridview and continues the flow. When the user presses the Home Button I want to do something in such a way that when he starts [clicks in the .apk again] the application restores his PHP session id, identifying that he was logged, and goes straight to this other activity that manages the gridview...
In few words: the user press home button and when start the application again he is already logged in and goes directly to the activity he was...
You need to go through this which explains the different available data-storage mechanism in android.
For your problem the answer is here. You save a preference and when the user restarts you can check for the preference value and do whatever is needed.
Related
I want to make an android app where the user first has to enter the password and then is taken to the subsequent activities.
But the user is able to access the last opened activity through recent apps without entering password.
So I want that every time the app is opened through recent tabs the user gets guided back to the main_activity where he first has to enter the password.
A good option would be to logout the user whenever your other activities go onPause(), and then when the activities are created, send them to the password activity if they are logged out.
Can you please explain to me why my activity which is currently (onStop) is go to OnDestroy event and restarting again after clicking on the app icon in the interface.
Because of that i have problem with user data saving. I'm using event OnDestroy to ask user if he want to save the data. Because of my misunderstanding when i collapse the app and click to it again i am waiting to my activity to have all the data in it but instead i see saving dialog from the previous activity whiсh goes to onDestroy event.
Other apps don't act like this.
What should i check?
Your description is not very clear, so my answer may not help you.
Research the app life cycle here: https://developer.android.com/guide/components/activities/activity-lifecycle.html
Your app will enter onStop is before onDestroy. This can happen if user activates another app. (So the user may no longer see any prompts that you display e.g. To ask to save or not.) Android may choose to destroy your app based on its rules after that point, even if the user clicks on the app icon to start it again.
Perhaps you can test what happens if the user switches back to the app (not clicking the icon), but use the "Recent apps" button and select it.
The main activity that starts when my app launches is shown with a bunch of menu items but access to some of the activities that are launched by some of the menu items require a username and password. After the user enters their credentials correctly, they have access to those activities. They can also use the back button and return to the main activity where the menu is located and I don't require them to re-enter their credentials again, as long as they remain within the app.
If however they hit the Back button while in the main activity, I terminate the app with finish();
The problem I am seeing is that if the user presses and holds the Home button, the list of apps is shown and if they tap on my app, they could end up being taken to one of the "secured" activities that requires a username and password. This can happen if they were using one of those activities and hit the Home button.
The solution I came up with is just to use a global variable that indicates that they are signed in and clear this when they hit the Back button from within the main activity.
But I need to check for this flag in the onResume of every activity to see whether they are signed in or not and if not, I do a finish() in the onResume preventing them from accessing the activity.
Is there a better way of doing this? Perhaps a way of terminating all the activities that are spawned when the user enters a "secured" activity? Or better yet, terminate all activities within my app? As it stands, I have a lot of activities and repeating the code in my onResume seems kind of senseless.
I have three activities, lets call them Act1, Act2 and Act3. There is a login form on Act1 which (upon successful login) goes to Act2 which has two options (1. Go to Act3, Go to Act3 with some extra data). The user then goes to Act3.
Of course, when the user presses the "home" button the android device, the application is minimized and held in memory until android needs to use the memory (in which case the App is destroyed). However, when the user presses the "home" button and then opens the app up again quickly, the app is restored to the Activity that was in the foreground before it was minimized.
I want to be able to minimize the app, and then once re-opened go straight to Act1 to prompt the user to login again. I do not want the app to be able to resume in Act2 or Act3.
Unless your application is really security-sensitive, the default behavior should be better for the user: typing their login and password every single time they launch the app can be very annoying. Take for example the native GMail application: it doesn't require you to reauthenticate every time it opens.
Now, if your application really needs that behavior (say it's a credit card safe or something like that), then my first guess would be to handle Act3's onPause() and call finish() from there. Just be careful not to call finish() twice (see isFinishing()).
Also, since this is a breakage of the user's expectations, make it clear to the user that your app behaves like that for their security, not because it wants to be annoying.
When the user moves away from your activity (pressing the home button for example), the onPause() method is called first. You should be able to handle your logic there (for example, calling finish() on Act2 or Act3).
Edit: heh, yeah, what he said :D
I have two activities: Login and List.
When i log-in i display the list. When user is in the list activity and press the home button. After that clicks on the app icon it brings to login page for some times only. While it should display the list.
How to resolve this?
Your app is pushed out of memory sometimes in order to free memory for other apps. When it happens, app is re-launched when user enters it and first Activity is opened. The only(or not the only) way for you to maintain your app state is to store it somewhere. You could save whether user logged in or not in SharedPreferences and when Login Activity is created you could check this.
Sometimes your app will be killed depending on how much memory other applications need. Your application must save and restore its state in order to behave as if it hadn't been killed.
If you read the section on the Activity Lifecycle in the developer docs, you should understand what you need to do.