0

I'm new to android development

In my application I want to keep log-in status of user in my activity. I have 2 edit boxes for entering name and password. And 1 check-box for user if he wants to keep log-in.

When user clicks on login button I'm saving name and password in shared preference. And in onCreate method I want to retrieve that value. If user has clicks check-box then he should directly redirect to next page. But its not working.

Here is my code:

signUpButton=(Button)findViewById(R.id.signUpLoginId);          
signUpButton.setOnClickListener(this);

//toggleButtonOnOff=(ToggleButton)findViewById(R.id.togglebutton);

loginEditText = (EditText) findViewById(R.id.loginNameID);          
passwordEditText=(EditText)findViewById(R.id.passwordId);           
box=(CheckBox)findViewById(R.id.checkbox);

preferences=PreferenceManager.getDefaultSharedPreferences(context);

String n=preferences.getString(PREF_USERNAME, null);                
String p=preferences.getString(PREF_PASSWORD, null);

loginEditText.setText(n1);
passwordEditText.setText(p1);

Intent intent1=new Intent(LoginActivity.this,FindFishMenuActivity.class);                   
startActivity(intent1);

LoginActivity.this.finish();

if(box.isChecked())
{
    box.setChecked(true);
    Toast.makeText(LoginActivity.this,"box value set to true",Toast.LENGTH_LONG).show();
    password=passwordEditText.getText().toString();
    preferences=context.getSharedPreferences(key_str, MODE_PRIVATE);
    editor=preferences.edit();
    editor.putString(PREF_USERNAME, name);
    editor.putString(PREF_PASSWORD, password);
    editor.commit();
    intent=new Intent(LoginActivity.this,FindFishMenuActivity.class);
    startActivity(intent);
    LoginActivity.this.finish();
}
inazaruk
  • 74,247
  • 24
  • 188
  • 156
android
  • 386
  • 1
  • 8
  • 21

1 Answers1

3

If you finish your Activity, the Code after it is never reached!

But I would put the code which saves your Name and Password to the SharedPreference before you fire the Intent. Maybe the Preference-Object isn't saved yet when you try to read from it in the other Activity. Try debuging this using LogCat.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • 2
    What makes you think so? Its not `return`. – inazaruk May 17 '11 at 08:22
  • Hm. Seems that you're right. See [here](http://stackoverflow.com/questions/2590947/about-finish-in-android). – Lukas Knuth May 17 '11 at 08:28
  • 1
    You also may want to check the source code: http://codesearch.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/app/Activity.java&q=package:android.git.kernel.org%20file:android/app/Activity.java&l=3241. There is no magic behind this method. – inazaruk May 17 '11 at 08:29