在主要活动中单击后退按钮后,它将返回登录,如何停止再次登录

时间:2016-02-01 14:38:43

标签: android

我对android非常新,我的登录活动工作正常,但我的问题是在登录到我的应用程序之后进入主要活动,进入我的主要活动后,当我按回按钮它将再次登录页面请任何人帮我解决这个问题

login.class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);


    sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);


    String checkemail=sharedpreferences.getString("Email", "");
    String checkuid =sharedpreferences.getString("Uid", "");

    if (checkemail.length()>0 && checkuid.length()>0){
        Intent main = new Intent(getApplicationContext(),MainActivity.class);
        startActivity(main);
    }


    username       =(EditText)findViewById(R.id.input_email);

    password       =(EditText)findViewById(R.id.input_password);

    login          =(Button)findViewById(R.id.btn_login);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkvalid();
        }
    });

    signup_link=(TextView)findViewById(R.id.link_signup);
    signup_link.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent signup= new Intent(getApplicationContext(),SignUp.class);
            startActivity(signup);
        }
    });

}
private void checkvalid()
{
    uname   =username.getText().toString();
    upassword=password.getText().toString();
    new AttemptLogin().execute();
    if (username.length()==0 || password.length()==0)
    {
        Toast.makeText(getApplicationContext(), "Please enter all fields", Toast.LENGTH_LONG).show();
    } else
    {
        uname   =username.getText().toString();
        upassword=password.getText().toString();
        new AttemptLogin().execute();
    }
}

class AttemptLogin extends AsyncTask<String, String, String>
{
    /** * Before starting background thread Show Progress Dialog * */

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Login.this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }


    @Override
    protected String doInBackground(String... args)
    {

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("email",uname));
        postParameters.add(new BasicNameValuePair("password",upassword));


        System.out.println("?????????" + postParameters);
        String response = null;
        try
        {
            response = SimpleHttpClient.executeHttpPost(LOGIN_URL,postParameters).toString();
            System.out.println("@@@@@@@@@@@@@@@@@"+response);
        }

        catch (Exception e)
        {
            e.printStackTrace();
            String errorMsg = e.getMessage();
        }
        return response;
    } /** * Once the background process is done we need to Dismiss the progress dialog asap * **/
protected void onPostExecute(String response)
{
    pDialog.dismiss();


    try {
        JSONObject jsonobject = new JSONObject(response);

        status = jsonobject.getString("Status");
        message = jsonobject.getString("Message");


        if (message.equalsIgnoreCase("OK")){
            JSONArray childArray = jsonobject.optJSONArray("Result");

            if (childArray != null && childArray.length() > 0) {

                for (int k = 0; k < childArray.length(); k++) {
                    JSONObject objj = childArray.optJSONObject(k);
                    uid        = objj.getString("id");
                    user_name  = objj.getString("user_name");
                    user_email = objj.getString("user_email");
                    user_status = objj.getString("status");
                    user_mobile = objj.getString("mobile_number");

                    System.out.println("AAAAAAAAAAAAAAAAAA"+user_name+user_email+user_mobile);
                    SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putString("Email",  user_email);
                    editor.putString("Uid",    uid);
                    editor.putString("Uname",  user_name);
                    editor.putString("Status", user_status);
                    editor.putString("Mobile", user_mobile);
                    editor.commit();
                    Intent login= new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(login);
                }
            }


            else{
                Toast.makeText(Login.this, "Registration Failed", Toast.LENGTH_SHORT).show();
            }

        }

    } catch (JSONException e)
    {
        e.printStackTrace();
    }
}
}

}

1 个答案:

答案 0 :(得分:4)

如果您不想在后退时加载上一个活动,则应在启动新活动后调用finish()方法。

登录成功后,您需要finish()登录活动。

添加

finish();

在方法startActivity();

的末尾

您可能需要根据您的要求添加任何条件。