按返回使登录表单无法访问

时间:2014-03-05 09:23:15

标签: android android-intent android-asynctask

我有一个登录表单,在用户输入正确的用户名和密码信息后,他们将被带到欢迎表单。我不希望用户从应用程序的任何位置返回登录表单而不先注销。他们返回登录表单的唯一方法是按回来。我怎么能阻止它?

编辑: 以下是执行登录的代码。

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);

            }

        }
    }

3 个答案:

答案 0 :(得分:4)

将标记Intent.FLAG_ACTIVITY_CLEAR_TOP添加到您的意图中...它将从活动堆栈中删除SplashActivity

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

,当您声明活动时,您可以通过在true文件中将android:noHistory设置为Manifest.xml来执行相同的任务... < / p>

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

<强>更新

通过添加一个以AsyncTask作为参数的构造函数来更新您的Context,如下所示......

   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();

答案 1 :(得分:1)

<强> WAY-1

您可以从AndroidManifest.xml文件

实现此功能

只需在android:noHistory="true"中添加LoginActivity属性。

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);

答案 2 :(得分:1)

您应该使用透明活动作为应用的启动器活动,我们称之为简介。

在OnResume()方法中,您可以根据注册状态决定要启动哪个活动。

请务必在您的简介活动和您决定启动的活动中设置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);