【问题标题】:Android: Session is restored when back is pressed twiceAndroid:按两次后退会恢复会话
【发布时间】:2015-02-13 01:18:38
【问题描述】:

我正在使用 android 制作登录应用程序。 Evrything 工作正常,但问题是当两次按下后退按钮时,会话注销将恢复?问题出在哪里

登出代码 sn-p

public void logoutUser(){
        // Clearing all data from Shared Preferences
        editor.clear();
        editor.commit();


        Intent i = new Intent(_context, Login.class);
        // Closing all the Activities
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


        _context.startActivity(i);
    }

调用注销方法

btnLogout.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View view) {
                        // Launching All products Activity
                        session.logoutUser();
                        Intent i = new Intent(getApplicationContext(), Login.class);
                        startActivity(i);

                    }
                });

登录代码:

nameValuePairs = new ArrayList<NameValuePair>(2);

            nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
            nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim())); 
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            response=httpclient.execute(httppost);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);
            //System.out.println("Response : " + response); 
            runOnUiThread(new Runnable() {
                public void run() {
                    //tv.setText("Response from PHP : " + response);
                    dialog.dismiss();
                }
            });
             String username = et.getText().toString().trim();
            if(response.equalsIgnoreCase("User Found")){
                  session.createLoginSession(username);
                runOnUiThread(new Runnable() {
                    public void run() {
                        //Toast.makeText(Login.this,"Login Success", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(Login.this, AndroidTabLayoutActivity.class));
                    }
                });


            }else if(response.equalsIgnoreCase("Not Employee")){
                showAlertNotEmployee();
            }else if(response.equalsIgnoreCase("Disabled")){
                showAlertDisabled();
            }else{
                showAlertNotFound();                
            }

        }catch(Exception e){
            dialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
        }
    }
    public void showAlertNotFound(){
        Login.this.runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
                builder.setTitle("Login Error.");
                builder.setMessage("User not Found.")  
                       .setCancelable(false)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                           }
                       });                     
                AlertDialog alert = builder.create();
                alert.show();               
            }
        });
    }

    public void showAlertDisabled(){
        Login.this.runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
                builder.setTitle("Login Error.");
                builder.setMessage("Account Disabled.")  
                       .setCancelable(false)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                           }
                       });                     
                AlertDialog alert = builder.create();
                alert.show();               
            }
        });
    }

    public void showAlertNotEmployee(){
        Login.this.runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
                builder.setTitle("Login Error.");
                builder.setMessage("Employee Account Only")  
                       .setCancelable(false)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                           }
                       });                     
                AlertDialog alert = builder.create();
                alert.show();               
            }
        });
    }

}

【问题讨论】:

    标签: android session login


    【解决方案1】:

    尝试添加到 Activity 标签中的 androidmanifest.xml

     android:noHistory="true"
    

    【讨论】:

    • 哇,它做到了。你能解释一下这段代码到底是做什么的吗?
    • 这使操作系统意识到它需要在不使用时丢弃活动。如果您想对片段执行相同的操作,则使用片段管理器弹出堆栈
    • 您好,我想回复您,尽管现在已经过去了几个星期。首先我想再次感谢你,但我遇到了一个问题。事实证明,即使使用简单的后退命令或切换应用程序,会话也会被终止。显然,一旦不使用就丢弃活动不是我想要的结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2015-10-03
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多