2015-04-19 46 views
1

我想知道關於android的設計問題。我經常有兩項活動將我的登錄和我的主要活動分開(側注:主要活動通常是一個導航抽屜)。我流通常是這樣的:簡單的Android設計流程查詢登錄活動和主要活動

我想這個變化來的:

人怎麼想的? 我的問題:

  1. 哪個活動真的應該在發射器抽屜裏?
  2. 我存儲身份驗證信息,無論他們是否登錄到SharedPreferences中。這是如何執行checkLogin()以及在成功嘗試登錄()之後存儲數據的位置。
  3. 我不希望用戶能夠按後退按鈕訪問登錄。只有按下指定的「註銷」按鈕才能使其返回登錄活動enter image description here

這對於零件一些代碼的示例

部分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(); 
+0

在啓動MainActivity的Intent時,只需在LoginActivity中調用finish(),用戶將無法返回到LoginActivity。 – Endzeit

+0

我忘了補充一點。這就是我所做的。 – napkinsterror

回答

2

我推薦下面的命令。

  1. 啓動LoginActivity第一
  2. 檢查用戶是否登錄
    • 沒有登錄?繼續3.)
    • 登錄?繼續4.)
  3. 提示您的用戶登錄。回到2)
  4. 呼叫完成()在你的LoginActivity並開始MainAcitivty

調用完成()將關閉LoginActivity,從而使其無法爲用戶取回它通過backpress。

反過來也可以實現註銷功能 - 在MainActivity上調用finish()並再次啓動LoginActivity。