I am developing an android app for the first time and I wanted to make the sessions for login and logout. I saw that most of the people suggested using SharedPreferences. But how can I check if the user logged out? If the user does not and clicks on my app, then the sign in page won't show up! The user can immediately go to the main page.
Here is my java class file where the user id and password would be asked...
package example.hiitattendance;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginPage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final EditText et = (EditText) findViewById(R.id.editText1);
final EditText et2 = (EditText) findViewById(R.id.editText2);
Button b = (Button) findViewById(R.id.button1);
final TextView tv = (TextView) findViewById(R.id.textView3);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String username = String.valueOf(et.getText());
String password = String.valueOf(et2.getText());
if(username.equals("Junaid") && password.equals("hiit")){
Intent intent = new Intent(LoginPage.this, JunaidHashmani.class);
startActivity(intent);
}
else if(username.equals("Fahad")&& password.equals("director")){
Intent intent = new Intent(LoginPage.this, FahadAzim.class);
startActivity(intent);
}
else if(username.equals("Adnan")&& password.equals("hiit")){
Intent intent = new Intent(LoginPage.this, AdnanSiddiqui.class);
startActivity(intent);
}
else if(username.equals("Shams")&& password.equals("hiit")){
Intent intent = new Intent(LoginPage.this, ShamsUlArfeen.class);
startActivity(intent);
}
else if(username.equals("Abdul Haseeb")&& password.equals("labtrainer")){
Intent intent = new Intent(LoginPage.this, AbdulHaseeb.class);
startActivity(intent);
}
else {
tv.setText("Error Logging in");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login_page, menu);
return true;
}
}
Now the code below will be the logged in page, where the user will be redirected after logging in.
package example.hiitattendance;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class FahadAzim extends ListActivity implements OnItemClickListener {
public static final String PREFS_NAME = "LoginPrefs";
ListView lv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fahadazim);
lv1 = (ListView) findViewById(android.R.id.list);
lv1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.fCourses)));
lv1.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent intent;
switch (arg2) {
case 0:
intent= new Intent(this,Bee_form.class);
startActivity(intent);
break;
case 1:
intent= new Intent(this,SNS_Form.class);
startActivity(intent);
break;
}
}
}
Kindly help me out...
i am a learner...
Need a well described answer from u mates:)