I am wondering about a design question about android. I often have two activities to separate my login and my main activity (side note: main activity is usually a navigation drawer). I flow is usually like this:
I was thinking of changing this to:
What do people think? My issues:
.This is an example of some code for the parts
Part A:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkLogin();
uprintDatabaseHelper = new uprintHelper(this);
uprintDatabaseHelper.getWritableDatabase();
setContentView(R.layout.activity_main_nav_drawer);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
Part B:
SharedPreferences sharedPreferences = getSharedPreferences("UserData",MODE_PRIVATE);
if(!sharedPreferences.getBoolean("LOGGED_IN", false)) {
if(!sharedPreferences.getBoolean("DATABASE_EXIST", false)) {
uprintDatabaseHelper = new uprintHelper(this);
uprintDatabaseHelper.getWritableDatabase();
}
Intent intent = new Intent(this,LoginActivity.class);
startActivity(intent);
}
Part C:
final boolean attemptLogin(String user, HttpClient client) {
HttpPost post = new HttpPost(getString(R.string.authorization_url));
String body;
body = "email=" + user;
post.setHeader("Content-type","application/x-www-form-urlencoded");
try {
post.setEntity(new StringEntity(body,"UTF-8"));
try {
HttpResponse response = client.execute(post);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
try {
JSONObject finalResult = new JSONObject(json);
return finalResult.getBoolean("success");
} catch (JSONException e1) {
e1.printStackTrace();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return false;
}
Part D:
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("LOGGED_IN", true);
editor.commit();
finish();
I'd recommend the following order.
Calling finish() will close the LoginActivity and thus make it impossible for the user to get back to it via backpress.
You can implement the log out function vice versa - call finish() on your MainActivity and start your LoginActivity again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With