Make a activity screen which shows up only once - java

I want to make a activity in an app which shows up only when the user installs the app and never again. How can I do that?

1.store value in sharedPrefernces.
SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("screen_show", false);
editor.commit();
2. get value from sharedPreferences
SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
preferences.getBoolean("screen_show", false);
3.It is false first time always
if( ! preferences.getBoolean("screen_show", false)){
// if show screen
Intent showscreenIntent=new(this,ShowScreen_Intent.class);
startActivity(showscreenIntent);
} else {
//
}
4.after showing screen first time set true in shared prefernece like this.
SharedPreferences preferences = this.getSharedPreferences("SoldiPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("screen_show", true);
editor.commit();
Now whenever step 3 execute else condition run and Activity will never show again.
hope it helps !!!

My issue solved by trying out this
boolean isFirstRun = getSharedPreferences("Preference", MODE_PRIVATE).getBoolean("isfirstrun",true);
if(isFirstRun){
getSharedPreferences("Preference", MODE_PRIVATE).edit().putBoolean("isfirstrun",false).commit();
Intent IntentFirstRunAct = new Intent(MainActivity.this,FirstRunActivity.class);
startActivity(IntentFirstRunAct);
}

Related

Get the state of a Switch in onCreate()

So my question is how I can get the state of a Switch.
I have this Code:
notificationSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(notificationSwitch.isChecked()){
System.out.println("Checked");
SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit();
editor.putBoolean("value", true);
editor.apply();
notificationSwitch.setChecked(true);
}else{
System.out.println("not checked");
SharedPreferences.Editor editor = getSharedPreferences("save", MODE_PRIVATE).edit();
editor.putBoolean("value", false);
editor.apply();
notificationSwitch.setChecked(false);
}
}
});
The state of the switch is saved in the Shared Preferences and if I start the app the Switch is on the same state as when I left the app. But when I start the app is should print checked or not checked but it doesn't print out anything
I will answer based on the title:
To get the state of the switch in onCreate():
//in onCreate:
SharedPreferences prefrences = getSharedPreferences("save", MODE_PRIVATE);
//false mean if not found set to false as default
boolean stateSwitch = prefrences.getBoolean("value",false);
//set state for switch
notificationSwitch.setChecked(stateSwitch);
//log the state
if(stateSwitch == true){
Log.d("TAG","checked");
}else{
Log.d("TAG","not checked");
}
UPDATE
keep your onclicklistener the same, and add the code I provided before your onclicklistener in onCreate:
1) when you turn switch on and leave activity and come back it must stay on.
2) when you turn switch off and leave activity and come back it must stay off.
3) if you do nothing leave activity and come back it must stay off.

How to skip LoginActivity after a successful try the first time you run the Android app

I'm new to Java/Android programming. I'm getting to know how the layout works and how to redirect it. I've got a splash screen > login > main . Now I want to skip the login activity. How can I do it ?
Edit:
This is what my code looks like now, after answer:
SharedPreferences prefs = getSharedPreferences("myPref", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("Bool", true);
editor.commit();
How I check from my Splashscreen:
SharedPreferences prefs = getSharedPreferences("myPref", 0);
if (prefs.getBoolean("Bool", false)) { //login_activity; }
else{ //maint_activity;
I think this would work.
You can use SharedPreferences to store some boolean value like skipLogin. During splash screen you can check this value, and display login or main, depending on result.
Simple logic, during the splash screen run the following method:
if(login){
redirects to main
}
else{
redirects to login
}

Android Java sharedPreferences logic issue

Android Java sharedPreferences logic issue
I have an app that has a sign in and sign out in it.
I want, when the user first downloads the app, it is sign out. Then when he signs in using the button, a sharedPreference value saves. Now next time he opens the app, it will automatically sign him in. Lets say he signs out then closes the app, so now when he opens it, it will sign him out. Thats how it goes like most apps.
What I did, at the first of the activity I added this
SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = SiggnedIn.edit();
editor.putBoolean("SiggnedIn?", false);
editor.commit();
then when the user clicks the button to signs out
SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = SiggnedIn.edit();
editor.putBoolean("SiggnedIn?", false);
editor.commit();
and if he clicks it to sign in
SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = SiggnedIn.edit();
editor.putBoolean("SiggnedIn?", true);
editor.commit();
and at last, if I have a method that updates everything every second.
so in it, I add
SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
boolean myIntValue = SiggnedIn.getBoolean("SiggnedIn?", false);
if(myIntValue){
SignHimIn();
}
That doesn't work.
This is not supposed to work, because each time the app is launched, you are overwriting SiggnedIn? to false.
SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = SiggnedIn.edit();
editor.putBoolean("SiggnedIn?", false);
editor.commit();
I would suggest using another key for SharedPreferences to identify if the app is launched for the first time.
SharedPreferences SiggnedIn = getSharedPreferences("YesOrNo", Activity.MODE_PRIVATE);
if (SiggnedIn.getBoolean("APP_LAUNCHED_FIRST_TIME", true)) {
SharedPreferences.Editor editor = SiggnedIn.edit();
editor.putBoolean("APP_LAUNCHED_FIRST_TIME", false);
editor.commit();
//For the first time, user should be signed out
editor.putBoolean("SiggnedIn?", false);
editor.commit();
}
Then in the Activity launch, check if user is signed in or not.
if (SiggnedIn.getBoolean("SiggnedIn?", false)) {
SignHimIn();
}
Rest Sign in and Sign out logic is Ok. I assume that you are doing this in Button click.
Keep things simple
Check Status
SharedPreferences pref = getSharedPreferences("userSession", Activity.MODE_PRIVATE);
boolean isLoggedIn = pref.getBoolean("isLoggedIn", false);
Sign In
SharedPreferences pref = getSharedPreferences("userSession", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLoggedIn", true);
editor.commit();
Sign Out
SharedPreferences pref = getSharedPreferences("userSession", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLoggedIn", false);
editor.commit();
Simple Logic
protected boolean isUserSignedIn(){
boolean isLoggedIn = false;
SharedPreferences pref = getSharedPreferences("userSession", Activity.MODE_PRIVATE);
isLoggedIn = pref.getBoolean("isLoggedIn", false);//false is just default
return isLoggedIn;
}
Usage
if(!isUserSignedIn()){
SignHimIn();
}
Why are you setting the boolean to false when you first launch your activity? You should just rely on the default value of getBoolean (which you defined as false) - This means that if you didn't put anything yet in the SignedIn field it will still return false.
Solution:
Inside your login button
SharedPreferences mPrefs= getSharedPreferences("mPrefsName", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("isSignedIn",true);
editor.commit();
When your activity starts (notice the false next to isSignedIn - this is the default value)
SharedPreferences mPrefs= getSharedPreferences("mPrefsName", Activity.MODE_PRIVATE);
if(mPrefs.getBoolean("isSignedIn",false))
DoStuff();
And inside logout button (NOT on activity start)
SharedPreferences mPrefs= getSharedPreferences("mPrefsName", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("isSignedIn",false);
editor.commit();

How to hide an activity after login in android?

I have developed an App where the user has to login into the app using the login Activity. After successful login, the app is directed to Activity B. The app doesn't provide any signout options.So next time when the user uses the app I want to hide the login Activity's layout. Is it possible to do so?If yes, how is it possible?
In LoginActivity do as Suggested by nr4bt but instead of int Put & get Boolean Value in SharedPreference.
write this code
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putValue("loginStatus", true);
editor.commit();
& onCreate() of LoginActivity Write this code
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String restoredText = prefs.getBoolean("loginStatus", false);
if (restoredText == true) {
// start activity
finish(); // destroy login so user can't come back with back button
}
Hope this helps you.
Keep a variable in sharedpreferences and check it when your login activity starts
in LoginActivity, when the user press login button and if success,
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putInt("loginStatus", 1);// removed ""
editor.commit();
Then in onCreate check the variable,
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int restoredText = prefs.getInt("loginStatus", 0);
if (restoredText == 1) {
// start activity
finish(); // destroy login so user can't come back with back button
}
It is quite simple to achive with SharedPreferences. You just save that user has loged in and later on start check if he already done it or not and act properly.

SharedPreferences not Reading

I am getting weird problem with my app.
I have set-up one SharedPreference, like this
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
flag = prefs.getBoolean("handle_calls", false);
if (flag) {
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.putBoolean("checkFlag", true);
editor.commit();
}
it was working fine some days before, but now this code is working fine but when I try to read these preferences in my SmsReceiver Class SharedPreferences doesnt read these values and default values are read (false)...Sometimes it work and most of the time it doesnt work at all!
Here is how I am reading the SharedPreferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SmsManager sms = SmsManager.getDefault();
//flag = prefs.getBoolean("handle_calls", false);
checkFlag = prefs.getBoolean("checkFlag", false);
checkDecisionFlag = prefs.getBoolean("checkDecisionFlag", false);
This checkDecisionFlag is working fine but checkFlag is taking default values.
Really pissed! Please Help!
remove the editor.clear(); from if condition. as clear will clear all the data from SharedPreference.
if (flag) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("checkFlag", true);
editor.commit();
}
Solved it!
This code was fine, instead the problem was with the life-cycle oriented. I had one another SharedPreference on which this depend. That was getting false again and again.
I think you wanted to use if (!flag) (not flag) on the third line of the first code sample.

Categories