1
我想知道關於android的設計問題。我經常有兩項活動將我的登錄和我的主要活動分開(側注:主要活動通常是一個導航抽屜)。我流通常是這樣的:簡單的Android設計流程查詢登錄活動和主要活動
我想這個變化來的:
人怎麼想的? 我的問題:
- 哪個活動真的應該在發射器抽屜裏?
- 我存儲身份驗證信息,無論他們是否登錄到SharedPreferences中。這是如何執行checkLogin()以及在成功嘗試登錄()之後存儲數據的位置。
- 我不希望用戶能夠按後退按鈕訪問登錄。只有按下指定的「註銷」按鈕才能使其返回登錄活動。
這對於零件一些代碼的示例
部分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));
}
部分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);
}
部分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;
}
D部分:
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("LOGGED_IN", true);
editor.commit();
finish();
在啓動MainActivity的Intent時,只需在LoginActivity中調用finish(),用戶將無法返回到LoginActivity。 – Endzeit
我忘了補充一點。這就是我所做的。 – napkinsterror