-1

when a user exit my android app without clicking log out button the user still appears logged in in my online database, when next he opens the app he will be directed to the log in page and he can't log in since he appears logged in in the database, please how do i solve this problem, am using the 1 and 0 form of log in. thanks

enter code here

public class MainActivity extends Activity {

private EditText username;
private EditText password;
private TextView reg;
//private TextToSpeech myTTS;
    //status check code
//private int MY_DATA_CHECK_CODE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    reg = (TextView)findViewById(R.id.reger);

      username = (EditText)findViewById(R.id.UserText1);
     password = (EditText)findViewById(R.id.PassText2);
  // String Userid;
    //String Passid= password.toString();
    Button login = (Button)findViewById(R.id.login);
    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
              final SharedPreferences userid = getSharedPreferences("prefs",0);
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", username.getText().toString()));
            postParameters.add(new BasicNameValuePair("password", password.getText().toString()));
            postParameters.add(new BasicNameValuePair("login", "1"));
            String response = null;
            try {
                response = CustomHttpClient.executeHttpPost("http://www.website.com/login4.php", postParameters);
                String res=response.toString();
                res = res.trim();
                res= res.replaceAll("\\s+","");   
                //reg.setText(res);
                //error.setText(res);
                if(username.getText().toString().equals("") || password.getText().toString().equals("")){

                    reg.setText("Sorry!! Incorrect Username or Password"); 
                }
                else if(res.equals(username.getText().toString()+password.getText().toString())){
                    SharedPreferences.Editor editor = userid.edit();
                    editor.putString("username", username.getText().toString());
                    editor.putString("password", password.getText().toString());
                    editor.commit();
                    //speakWords("You are logged in succesfully");

                startActivity(new Intent(MainActivity.this, HomePage.class));
                }

                else {

                    reg.setText(res);
                }
            } catch (Exception e) {
                reg.setText("You have a network failure");
            }

        }
    });


    reg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this, Registration.class);
            startActivity(intent);
        }
    });
    TextView freeview =(TextView)findViewById(R.id.Samp);
    freeview.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this, Sample_year.class);
            startActivity(intent);
        }
    });
}

// Set up an instance of SystemUiHider to control the system UI for }

?

  • Put an on close event in your app and call test for already logged out, if not do it... – Tony Hopkinson Dec 21 '13 at 17:37
  • 1
    Users don't exit android apps. They navigate away from them, in rare cases they force stop them, but they do not "exit" You are going to need to devise an expectation of behavior consistent with the Activity Lifecycle and then implement that. – Chris Stratton Dec 21 '13 at 18:00
  • @ ony Hopkinson Thanks but don't really get u – user3125701 Dec 25 '13 at 16:04

2 Answers2

0

change the value to 0 in the onStop() method of the activity class which is called when your user navigates away from the app!

Niraj Adhikari
  • 1,678
  • 2
  • 14
  • 28
0

For anyone else in the future I'm using firebase so this is what I did:

if (firebaseAuth.getCurrentUser()!= null)
{
    startActivity(new Intent(getApplicationContext(),NavActivity.class));
}
Farhad
  • 4,119
  • 8
  • 43
  • 66
Sammy
  • 1
  • 2