Activity that is only launched once after a new install? - android

I want my app to have an activity that shows instruction on how to use the app. However, this "instruction" screen shall only be showed once after an install, how do you do this?

You can test wether a special flag (let's call it firstRun) is set in your application SharedPreferences. If not, it's the first run, so show your activity/popup/whatever with the instructions and then set the firstRun in the preference.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if ( firstRun )
{
// here run your first-time instructions, for example :
startActivityForResult(
new Intent(context, InstructionsActivity.class),
INSTRUCTIONS_CODE);
}
}
// when your InstructionsActivity ends, do not forget to set the firstRun boolean
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == INSTRUCTIONS_CODE) {
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();
}
}

yes, you can fix this problem with SharedPreferences
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("firstrun", MODE_PRIVATE);
editor = pref.edit();
editor.putString("chkRegi","true");
editor.commit();
Then check String chkRegi ture or false

Related

if application re-open SharedPreferences not saving boolean

I have the following code in my project called menu.class
f1 =(Button)findViewById(R.id.f1);
f1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent intent = new Intent ();
intent.setClassName ("com.example.aplication", "com.example.application.levelone");
startActivityForResult (intent, 0);
}
});
}
public void onActivityResult (int requestCode, int resultCode, Intent intent) {
super.onActivityResult (requestCode, resultCode, intent);
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
switch (resultCode) {
case 11: f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
boolean levelTwoUnlocked = preferences.getBoolean("f2", true);
if(levelTwoUnlocked){
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
}
else {
f2.setVisibility(View.GONE);
f2lock.setVisibility(View.VISIBLE);
}
f2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
Intent intent = new Intent ();
intent.setClassName ("com.example.application", "com.example.application.leveltwo");
startActivityForResult (intent, 0);
}
});
}
The code is working fine. f2 button is set visible and f2lock invisible.
But when I re-open the application the f2 button back to visible.
Did my Preferences code not complete?
UPDATED
i had changed the preferences code like this
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor ed = preferences.edit();
boolean levelTwoUnlocked = preferences.getBoolean("f2", true);
ed.commit();
and when i re-open the application it still had the same problems
You are retrieving the value of f2, but it doesn't look like you're saving it anywhere.
preferences.getBoolean("f2", true);
This will return the last saved value of f2 or true if f2 doesn't exist (i.e. you've never saved it in your SharedPreference instance) - it won't create or save any value in the SharedPreferences instance for you.
To save the value, you need to create a SharedPreferences editor, set the value (or values) you want to save and commit it:
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("f2", levelTwoUnlocked);
editor.commit();
Now the next time you read the f2 value, it will have whatever value you last committed.

Logout clear SharedPreferences

I have a login page that saves username and password to SharedPreferences. I have another Activity class that includes a logout button. I want to clear SharedPreferences when I click the logout button. The problem is that I don't get the SharedPreferences from this class. How can I get the SharedPreferences?
LoginPage
public class MainActivity extends Activity {
public SharedPreferences.Editor loginPrefsEditor;
public SharedPreferences loginPreferences;
private Boolean saveLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.et_Username);
pass = (EditText) findViewById(R.id.et_Password);
login = (Button) findViewById(R.id.bt_Login);
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
name.setText(loginPreferences.getString("username", ""));
pass.setText(loginPreferences.getString("password", ""));
}
login.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
name1 = name.getText().toString();
pass1 = pass.getText().toString();
//new Thread (new Task()).start();
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("username", name1);
loginPrefsEditor.putString("password", pass1);
loginPrefsEditor.commit();
new myAsyncTask().execute();
}
});
}
Logout Button in AnotherActivity
logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Launching News Feed Screen
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
Try this !
logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Launching News Feed Screen
SharedPreferences preferences =getSharedPreferences("loginPrefs",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
finish();
});
I think you have a trouble in understanding Shared preferences in android .
According to official documentation
To get a SharedPreferences object for your application, use one of two
methods:
getSharedPreferences() - Use this if you need multiple preferences
files identified by name, which you specify with the first parameter.
getPreferences() - Use this if you need only one preferences file for
your Activity. Because this will be the only preferences file for your
Activity, you don't supply a name.
You should have a Context for using both the above methods .
Also Shared preferences are stored asa key value pair , so clearing should mean that you set the values to some empty string.
For more details , and better explanation you can read here http://developer.android.com/guide/topics/data/data-storage.html#pref
and http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
Hope this will help.
Cheers!
It as Simple. Like you save your data in SharedPrefernce
SharedPreferences sp = getSharedPreferences("MYKEY",0);
SharedPreferences.Editor editor = sp.edit();
editor.putString("username" , username);
editor.putString("password" , password);
Now you can retrieve as in any class of your app like,
SharedPreferences sp = getSharedPreferences("MYKEY",0);
String uname = sp.getString("username");
String pwd = sp.getString("password");
And for clear your username and password
editor.clear();
editor.commit();
or
editor.remove("username");
editor.remove("password");
editor.commit();
Why not write a SharedPreference utility class. This can be accessed from both the activities.
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear();
editor.commit();
In Kotlin you can use this code to clear the sharedpreference data
private val sharedPref = "sharedpreference"
val sharedPreferences: SharedPreferences = context.getSharedPreferences(sharedPref, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.clear()
editor.apply()
SharedPreferences preferences =getSharedPreferences("loginPrefs",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
finish();

usea savePreferences in dynamically layout -android

I'm making an app that contains a dynamically layout and it generates textView's in run time, I need that when I close the application all the textView has been added, don't erase; I think I can do that whit savePreferences(), but What parameters I have to use?
private void savePreferences(What parameter here, What parameter here) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.commit();
}
Thank you
Shared Preferences should work. Try this...
public class sample extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
#Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
// THIS IS WHERE YOU STORE THE TEXTVIEW DATA
// THERE ARE MANY ' editor.put...(...) ' methods
// ie. editor.putInt(...) , editor.putString(...)
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}

I want to do registration of my app at the time of installation?

Problems
The activity which will do registration will be shown only once
After registration the control should move to
next main activity
I have used following code
Below code will not meet my requirement?
Any Help will be appreciated!!
Code in Registration Activity
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("registration", true);
editor.commit();
Code in Main Activity
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean regComplete =prefs.getBoolean("registration", false);
SharedPreferences.Editor editor =prefs.edit();
editor.putBoolean("registration", false);
editor.commit();
if(regComplete)
{
startActivity(new Intent(this, SecureXActivity.class));
} else
{
startActivity(new Intent(this, LoginActivity.class));
}
Just put your Registration code in SecureXActivity.class
and Check for the Registration before setContentView(), if its not done then start LoginActivity.class
And in LoginActivity.class after registration complete put these code,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("registration", true);
editor.commit();
If you use this approach then I think you don't need Main Activity class..
And keep in mind this all thing done at time of your application's first run not at the time of installation.
The Registration Activity should be like this:
public class RegistrationActivity extends Activity {
public static SharedPreferences pref;
public static final String PREFS_NAME = "MyPrefsFile";
public void onCreate(Bundle savedInstanceState) {
pref = getSharedPreferences(PREFS_NAME, 0);
boolean regComplete =prefs.getBoolean("registration", false);
if(regComplete){
//go to main class
}else{
//stay in the registration class
}
}
}
and the Main class should be:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
RegistrationActivity.pref = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("registration", true);
// Commit the edits!
editor.commit();
}
}

Storing and Retrieving Values from SharedPreferences on Activity state changes

Basically I have a list and I need to remember the offset and load the offset value every time the Activity is restored unless the Activity completely destroyed.
//Inside onCreate
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
Offset = settings.getInt("TheOffset", 0);
//End onCreate
#Override
protected void onPause() {
super.onPause();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", Offset);
}
#Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", Offset);
}
#Override
protected void onDestroy() {
super.onDestroy();
//settings.getInt("TheOffset", 0);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", 0);
}
onPause() will always be called before your activity is placed in the background and/or destroyed, so you do not have to save state in onStop() and onDestroy() as well.
For the state to be preserved in SharedPreferences, you need to add editor.commit() after writing the value. Otherwise it won't be stored. Like this:
super.onPause();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", Offset);
editor.commit();
You can read more here: http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
You will only need to save your offset in onResume() and set it to 0 when the activity is going to be destroyed, which you can tell by using isFinishing() in onPause(), like the following:
protected void onPause() {
if(isFinishing()) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", 0);
editor.commit();
}
}
...but I still have no idea what you intend to achieve.

Categories

Resources