How to maintain Session Timeout handling in Android Application - android

I am new to Android App development.I have a small issue.Working on my Android Application suddenly hit "Home" Button so it goes to background after some time again go to "Home" and click my app icon it again getting started from my first screen instead of this i have keep the last viewed Activity and should show message like "Your Session timed out So Please Loging again the box with two EditText boxes" Then check the credentials and Allow the user to continue or redirect to Login Screen.How to achieve this.
Thanks in advance..

Its simple in Android. you need to maintain SharedPreferences.
Read this for better understanding of the concept.
This will work as
==> when ever you are trying to maintain login concept save user name in the sharedpreference.
==> once data is edited in prefernces.xml , the data in it can be check in any Activity.
For example, to save username, password and session ID, you can:
SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
edit.putString("User Name", username);
edit.putString("Password", password);
edit.putInt("Session ID", session_id);
edit.commit();
and get them
SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
username = pref.getString("User Name", "");
password = pref.getString("Password", "");
session_id = pref.getInt("Session ID", 0);

SharedPreferences is definately the way to go, but to go into more detail for the timeout:
Save the current timestamp in onPause() (-> SharedPreferences)
Then in onResume() compare it, to check for a timeout
If you did not time-out just continue, otherwise show that nice screen, simply logout, self-destruct the device, or whatever you would like to do in that case :-)
Also you might consider implementing some kind of session manager class, to handle it on a higher level:
Nice example/tutorial from android hive

See this diagram for an explanation of the Activity lifecycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Override the appropriate methods in your activity to keep track of when it loses/regains focus.

If you want to maintain the user login session then instead of having the preference in all the activities we can have handler inplace to do this.
Create Handler after the user logsin
Set sendMessageAtTime(Message, long), long is millisecond value. This will call the logout function that defined.

Related

Mandatory Settings Update

I am making one android app where one setting is must every user need to update that which is pincode.
So after successful login, if the user has not updated pincode yet, it needs to be updated mandatory before navigating to any other screen.
What is the best way to implement this? Any Ideas?
Update:
After answer, I meant to say that setting I will be fetching from firebase database as boolean flag. So my actual query is how to show that as a mandatory to get the user update? (i.e) either navigating to different activity or showing popup and getting the setting etc. I need UI design ideas.
What is the best practice?
It is not clear what is the point of this, and if you have a server side that controls that stuff, but I'll try to give a help.
If you have a Server controlling authentication:
On login, call the API of your service to check if has happened or not. You could save the answer in a "shared preference" (Android Documentation), so you don't call your API every time.
If you only want to have the application on a client side:
Use 1 to store values that indicate if the desired action was performed or not, which is verified right after the login.
EDIT:
If the action is mandatory, you could send the user to an activity to update the pin, this will happen until the action is performed.
Client side approach:
You can use SharedPreferences to persist a value, like a simple boolean, that will inform you if that the user already updated the pincode or not.
I would recommend you to perform the check in the onResume() of your Launcher Activity.
Putting it simple and explicit:
public static final String PREF_FILE_NAME = "PrefFileName";
public static final String PREF_IS_PIN_CODE_UPDATED = "PREF_IS_PIN_CODE_UPDATED";
#Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
Boolean isPinCodeUpdated = prefs.getBoolean(PREF_IS_PIN_CODE_UPDATED, false);
if (isPinCodeUpdated) {
// You can proceed
} else {
// Force the user to update the pin code, by prompting for instance a dialog where he can change the pin with setCancelable(false)
}
}
After you know that your user already updated the pin code you just need to set the preference to true.
SharedPreferences.Editor editor = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE).edit();
editor.putBoolean(PREF_IS_PIN_CODE_UPDATED, true);
editor.apply();
After this every time the user opens the app it will pass in the onResume() method and your isPinCodeUpdated var will be true and the app should proceed as usual.
From you question I am assuming that you didn't want a response in terms of UI but how could you have the information persisted.
Note: This implementation fails for multiple users in the same device. With few tweaks you can make it work for multiple users in the same device but in my opinion this verification should be done server side.

How to create pop-up when first open app in Android?

I write an application, I want to get a phone number, but getline1number() not working on any devices.
So, I want to create a pop-up to enter a phone number and submit to save and don't show in next time open app.
Like this:
You can always use SharedPreferences to do such things:
SharedPreferences sp = getSharedPreferences("FirstTimeFile", Context.MODE_PRIVATE);
/**
* when the app is opened for the first time, no such variable
* (appIsOpenedForTheFirstTime) exists. So, it becomes true.
*/
boolean appIsOpenedForTheFirstTime = sp.getBoolean("IsAppOpenedForFirstTime",true);
//since it is true, it will be set to false after the execution of following block:
if(appIsOpenedForTheFirstTime) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("IsAppOpenedForFirstTime", false);
editor.commit();
//PUT THE CODE FOR YOUR POPUP HERE
}
As the SharedPreferences values remain in the application data even after you close the app, so the next time you open the app, the value of appIsOpenedForTheFirstTime will be false and hence your pop-up code won't be executed.
Ah, as a side-note, if you clear the app data, everything gets cleared - including the SharedPreferences. Read this official article for in-depth understanding.

Android : Popup certain dialog at first startiup only

I am trying to develop an app that requires certain values to be set by the users at the app's first startup only, because i don't wanna bother them frequently inputting the values everytime they launch the app. My app has a single activity main and uses certain values that are inputted by the users at first startup. How can I make this possible.
Please explain me elaborately . :-)
You should use SharedPreferences to keep a track of the first use.
In the onCreate Method of your Activity (Startup activity), you could do something like this,
SharedPreferences userPrefs = getSharedPreferences("UserPrefs", 0);
Boolean firstUse = userPrefs.getBoolean("firstUse", true);
if(firstUse){
//this implies it is the first use of the app
//also once you are done implementing the logic for first use you need to put firstUse as true
SharedPreferences.Editor editor = userPrefs.edit();
editor.putBoolean("firstUse", false);
editor.commit();
}
else{
//take the user directly inside the app
}
Also, if you plan to save user information in the first use, look at different ways of storing data here.
show the alert initially and after getting the input values keep it in preference and next time check whether the required values existing or not. If it is already there avoid popup
For getting more information about shared preference check this link http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
preferences_statusFirst.getString("boot", "");
if (status.length() <= 0)
{
showDialog(DIALOG_birth);
editor_boot.putString("boot", "1");
editor_boot.commit();
}
else
{
}
}

How to pre populate data between the screens in android

I have an app with 3 screens.
For each screens i have next button in each screen to go to the next screen.
problem:
Scenario 1:
--> I filled the data in first screen
--> I go to the second screen.
--> When i come back to the first page i am able to see the data what i filled.
-->But when i again go to the second page i am not able to see the data what i filled in the second form.
How can i manage this in android?
Thanks in advance...
You can save the data in Shared preferences. Check this : Shared Preferences Android and Example
Hope this helps.
You need to save the state yourself using your activities' lifecycle methods. Read this: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
You can save temporary data in application data. Once in the onCreate method, you can reuse that data, or you can use an activity flag to ensure that there's only one instance of your activity in the backstack.
try
{
//in first run app. go to catch and in other go to try
SharedPreferences pref = getSharedPreferences("pref",0);
SharedPreferences.Editor edit = pref.edit();
String x= pref.getString("login", null);
edit.commit();
if(x.equals("first"))
{
//here you can fill editbox by value you entered before
edt1.setText(x);
//and make this way to all edittext
}
}
catch(Exception e)
{
SharedPreferences pref = getSharedPreferences("pref",0);
SharedPreferences.Editor edit = pref.edit();
edit.putString("login",edt1.getText().toString());
//edt1.getText().toString() value(s) you want to save
edit.commit();
Intent intent = new Intent(getApplicationContext(), First.class);
startActivity(intent);
}

How do i make dialog that pops up on second Run and after that

Basically i want a dialog which has text and 3 buttons
|| YES || NO || NEVER ||
I want it to pop up, only on second run and after that on every run until yes or never is selected.
I am sorry if this is a noobish question but i have no idea how to store data.
Is shared preferences the way to go, if so can any one give me an idea as to which function is to be called and from where.
I am having trouble understanding this part, if i write the data from the activity, it'll basically be over written every time the app runs.
SharedPreferences is the way to go. Take a look at this example.
What is your real question? If I am not wrong, you asking this question :
how to store data
how to show 2 button instead 3 button after some point.
So the answer is, you can store your data on SharedPreferences use it like this
SharedPreferences sp = act.getSharedPreferences(name, MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString(key, value); // put some data
editor.commit();
String val = sp.getString(key, defaultValue); // get some data, if it not exist, defaultValue will be returned

Categories

Resources