2014-03-05 22 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);