1

I have a login page for my project.Here my requirement is,that login page always want to be logged-in,if once i log in.I have got some ideas through Google,ie., it recommended me to use shared-preference concept,right now i am following this concept and i have tried some code.

In my project the problem is after giving the proper username and password,it does not switch to another screen,at the same i am getting nothing on my log-cat too.How to achieve this concept?

Suggestions please..

please find my sources for reference

class SaveSharedPreferece

public class SaveSharedPreference 
{
static final String PREF_USER_NAME= "username";

static SharedPreferences getSharedPreferences(Context ctx) {
    return PreferenceManager.getDefaultSharedPreferences(ctx);
}

public static void setUserName(Context ctx, boolean userName) 
{
    Editor editor = getSharedPreferences(ctx).edit();
    editor.putBoolean(PREF_USER_NAME, userName);
    editor.commit();
}

public static String getUserName(Context ctx)
{
    return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
}}

MainActivity.java

public class MainActivity extends Activity 
{
Button btn;
EditText edt1,edt2;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 }

 public void loginpage()
 {  
    edt1 = (EditText)findViewById(R.id.editText_username);
    edt2 = (EditText)findViewById(R.id.editText_password);

    btn = (Button)findViewById(R.id.button_login);
    btn.setOnClickListener(new View.OnClickListener()
    {

@Override
public void onClick(View v) 
{
    if(edt1.getText().toString().length()!=0 && edt1.getText().toString().length()!=0)
    {
        Intent intvar =  new Intent(v.getContext(), ResultActivity.class);
        startActivity(intvar);
    }
    else
    {
    Toast.makeText(getApplicationContext(), "oops! empty..", Toast.LENGTH_SHORT).show();
        }
    }
});

if(SaveSharedPreference.getUserName(MainActivity.this).length() == 0)
{
     // call Login Activity
     loginpage();
   }
 else
 {
    // Call Next Activity

 }

 if (getIntent().getBooleanExtra("EXIT", false)) 
 {
   finish();
        }
    }
}

ResultActivity.java

public class ResultActivity extends Activity 
{ 

Button btn_exit;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);

    btn_exit = (Button)findViewById(R.id.button_exit);
    btn_exit.setOnClickListener(new View.OnClickListener()
    {

    @Override
    public void onClick(View v) 
    {
        Intent intent = new Intent(ResultActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXIT", true);
        startActivity(intent);
        }
    });
}}

thanks for your precious time!..

Rohith
  • 991
  • 4
  • 18
  • 31
  • Check my [Answer][1] for this same.... [1]: http://stackoverflow.com/questions/12639899/shared-preferences-in-android/12640108#12640108 – Hardik Joshi Oct 06 '12 at 07:37

2 Answers2

0

You are saving boolean to SharedPreferences and getting String. How that is possible.

Better to save and get either boolean or String.

Change your setUserName() code in SaveSharedPreference class as below and it works

public static void setUserName(Context ctx, String userName) 
{
    Editor editor = getSharedPreferences(ctx).edit();
    editor.putString(PREF_USER_NAME, userName);
    editor.commit();
}
TNR
  • 5,839
  • 3
  • 33
  • 62
0

First add another field in SharedPrefernce file as password. Do same whatever you have done foe username(make it string instead of boolean in setter method) like setter and getter methods.

Create one method which will fetch values from sharedPrefernce like this :

private void validateUser(){
String username  = SaveSharedPreference.getUserName(MainActivity.this);
String password = SaveSharedPreference.getPassword(MainActivity.this);
 if (check_for_any_condition){
    Intent intvar =  new Intent(v.getContext(), ResultActivity.class);
    startActivity(intvar);
 }
}

Call this method in your oncreate method.

Hope it will help you.

Naresh J
  • 2,087
  • 5
  • 26
  • 39