2014-03-05 51 views
0

我有一個登錄表單,並且在用戶輸入正確的用戶和密碼信息後,他們將被帶到一個歡迎表單。我不希望用戶先從應用程序的任何位置返回登錄表單,而無需先登錄。他們可以返回登錄表單的唯一方法是按回。我怎麼能阻止呢?通過按回不登錄表格

編輯: 以下是執行登錄的代碼。

private class UserLoginTask extends AsyncTask<String, Void, User> { 

     @Override 
     protected User doInBackground(String... params) { 
      User user; 
      try { 
       user = RestCommunicator.authenticateUser(params[0], params[1]); 
      } catch (NetworkException e) { 
       user = new User(); 
       user.setResponseStatus("Network Error"); 
      } 
      return user; 
     } 

     @Override 
     protected void onPostExecute(User result) { 
      if (result != null && result.getUsername() != null 
        && !result.getResponseStatus().equals("Network Error") 
        && !result.getResponseStatus().equals("wrong-user") 
        && !result.getResponseStatus().equals("wrong-password")) { 
       Log.d("result", "*" + result.getResponseStatus() + "*"); 
       userDao.insertOrReplace(result); 

       SharedPreferences sharedPreferences = PreferenceManager 
         .getDefaultSharedPreferences(getBaseContext()); 
       Editor editor = sharedPreferences.edit(); 
       editor.putBoolean("loggedin", true); 
       editor.commit(); 
       super.onPostExecute(result);     
       Intent intent = new Intent(SplashActivity.this, 
         WelcomeActivity.class); 
       startActivity(intent); 
       finish(); 
      } else { 

       Intent intent = new Intent(SplashActivity.this, 
         LoginErrorActivity.class); 
       intent.putExtra("error", result.getResponseStatus()); 
       startActivityForResult(intent, 90000); 

      } 

     } 
    } 
+2

你能發表一些代碼嗎? – Blackbelt

回答

4

Intent.FLAG_ACTIVITY_CLEAR_TOP添加到你的意圖......它會從活動組中刪除SplashActivity

Intent intent = new Intent(SplashActivity.this, WelcomeActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent); 

OR,您可以通過在Manifest.xml文件中設置trueandroid:noHistory當你如下聲明你的活動做同樣的任務......

<activity 
     android:name=".SplashActivity" 
     android:noHistory="true" > 

更新:

通過添加一個以Context作爲參數的構造器來更新您的AsyncTask ...

private class UserLoginTask extends AsyncTask<String, Void, User> { 

     private Context mContext; 

     public UserLoginTask(Context context) { 

      this.mContext = context; 
     } 

     @Override 
     protected User doInBackground(String... params) { 
      User user; 
      try { 
       user = RestCommunicator.authenticateUser(params[0], params[1]); 
      } catch (NetworkException e) { 
       user = new User(); 
       user.setResponseStatus("Network Error"); 
      } 
      return user; 
     } 

     @Override 
     protected void onPostExecute(User result) { 
      if (result != null && result.getUsername() != null 
        && !result.getResponseStatus().equals("Network Error") 
        && !result.getResponseStatus().equals("wrong-user") 
        && !result.getResponseStatus().equals("wrong-password")) { 
       Log.d("result", "*" + result.getResponseStatus() + "*"); 
       userDao.insertOrReplace(result); 

       SharedPreferences sharedPreferences = PreferenceManager 
         .getDefaultSharedPreferences(getBaseContext()); 
       Editor editor = sharedPreferences.edit(); 
       editor.putBoolean("loggedin", true); 
       editor.commit(); 
       super.onPostExecute(result);  

       Intent intent = new Intent(mContext, WelcomeActivity.class); 
       mContext.startActivity(intent); 
       ((Activity) this.mContext).finish(); 

      } else { 

       Intent intent = new Intent(mContext, LoginErrorActivity.class); 
       intent.putExtra("error", result.getResponseStatus()); 
       mContext.startActivityForResult(intent, 90000); 

      } 

     } 
    } 

然後在下面的活動叫你的AsyncTask ...

UserLoginTask loginTask = new UserLoginTask(SplashActivity.this); 
loginTask.execute(); 
+0

但它看起來像這個不工作在sdk 14+ –

+0

都是選項失敗? –

+0

是的。兩個都。 –

1

WAY-1

您可以從您的AndroidManifest.xml文件實現這個

通過只需在LoginActivity中添加android:noHistory="true"屬性即可。

Android Reference Link

WAY-2從堆通過設定標誌Intent.FLAG_ACTIVITY_CLEAR_TOP

刪除LoginActivity。

Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent); 
1

您應該使用透明活動作爲應用程序的啓動器活動,我們將其稱爲「簡介」。

在OnResume()方法中,您根據註冊狀態決定要啓動哪個活動。

請記住在您的Intro活動和您決定啓動的活動上設置android:noHistory="true"。通過這種方式,啓動的活動不會存儲在活動堆棧中,您可以通過按back直接退出應用程序。

你的清單文件應該是這樣的:

<activity 
     android:name="com.example.Intro" 
     android:label="@string/app_name" 
     android:noHistory="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</activity> 
<activity 
     android:name="com.example.AuthActivity" 
     android:label="@string/title_activity_auth" 
     android:noHistory="true" > 
</activity> 
<activity 
     android:name="com.example.WorkingActivity" 
     android:label="@string/title_activity_working" 
     android:noHistory="true" > 
</activity> 

你介紹。OnResume:

Class toLaunch = null; 
//status String is the value stored after registration/authentication 
//you can store it using shared preferences 
     if(status.equals("invalid"){ 
      toLaunch = RegistrationActivity.class; 
     } else if (status.equals("registered")){ 
      toLaunch = AuthActivity.class; 
     } else if (status.equals("authOk")){ 
      toLaunch = WorkingActivity.class; 
     } else{ 
      Toast.makeText(this, "Bad Status", Toast.LENGTH_LONG).show(); 
        return; 
     } 
     Intent i = new Intent(Intro.this,toLaunch); 
     startActivity(i);