Run another action if checked

When my application starts, the "Login Activity" window appears. It contains the checkbox next to "Sign me up next time." I want to start another action the next time I start the application and send him the username and password of the user, if the checkbox is selected.

How to implement this?

+4
source share
3 answers

You probably want to save the username and checkbox in SharedPreferences. After that, you can restore them when the application starts.

Example:

//Saving the values SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("username",username); editor.putString("password",password); editor.putBoolean("isChecked", isCheckBoxChecked); editor.commit(); //Retrieving the values SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE); if(sharedPreferences.getBoolean("isChecked")) { //Do whatever you need to do if it checked } 
+4
source

Use SharedPreferences to save your username and password. When your activity starts, check if they are saved in Shared Preferences . U can find many good examples of Shared Preferences on google.

+1
source

In your account, do the following:

  pref = getSharedPreferences(PREFS_NAME, 0); intent = new Intent(context, LaunchingActivity.class); isChecked = pref.getBoolean("isChecked", false); if(isChecked) startActivity(intent); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if(username.equalsIgnoreCase("") && password.equalsIgnoreCase("")) { username = userEdtTxt.getText().toString(); password = pwdEdtTxt.getText().toString(); } if(checkBox.isChecked()) { editor = pref.edit(); editor.putString("username", username); editor.putString("password", password); editor.putString("isChecked", true); editor.commit(); } startActivity(intent); } }); 

Then in the starting action do it,

  SharedPreferences pref = getSharedPreferences(PREFS_NAME, 0); String username = pref.getString("username", ""); String password = pref.getString("password", ""); 
0
source

All Articles