我正在开发一个应用程序,并且仅在用户退出上一个会话时才希望使用户登录,但我无法这样做。每当我关闭程序并重新打开它时。即使我没有注销,它也会再次定向到主页。 z

  package in.co.medimap.www.medimap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static in.co.medimap.www.medimap.R.layout.login;

/**
 * Created by sony on 28-04-2016.
 */
public class login extends Activity {
TextView signup_text;
Button login_button;
EditText PHONE_NO,PASSWORD;
AlertDialog.Builder builder;
public static final String MyPREFERENCE ="Myprefs";
public static final String PHONE="phone";
public static final String PASS="password";
SharedPreferences sharedPreferences;



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



    setContentView(R.layout.login);
    signup_text=(TextView)findViewById(R.id.sign_up);
    signup_text.setOnClickListener(new View.OnClickListener(){

                                       @Override
                                       public void onClick(View v) {
                                           startActivity(new Intent(login.this,register.class));
                                       }
                                   });
    PHONE_NO=(EditText)findViewById(R.id.phone_no);
    PASSWORD=(EditText)findViewById(R.id.password);
    login_button=(Button)findViewById(R.id.login_button);

    login_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             String ph = PHONE_NO.getText().toString();
            String p=PASSWORD.getText().toString();


            if (ph.equals("")||p.equals("")) {

                builder = new AlertDialog.Builder(login.this);
                builder.setTitle("Something went wrong...");
                builder.setMessage("Please fill all the fields...");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();


                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
            else
            {
  //see from here
                sharedPreferences = getSharedPreferences(MyPREFERENCE,Context.MODE_PRIVATE );
                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putString(PHONE, ph);
                editor.putString(PASS, p);
                editor.commit();

                BackgroundTask backgroundTask = new BackgroundTask(login.this);
                backgroundTask.execute("login",ph,p);

            }







}
});
    }
}


这是我的家庭活动,在登录用户后
我想要,如果我没有注销,那么每当我打开我的应用程序时,都应该从这里开始

package in.co.medimap.www.medimap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HomeActivity extends Activity {

Button Logout = null;

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Logout = (Button) findViewById(R.id.Logout);
    textView = (TextView) findViewById(R.id.welcome_txt);
    String message = getIntent().getStringExtra("message");
    textView.setText(message);

    Logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences sharedPreferences = getSharedPreferences(login.MyPREFERENCE, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.commit();
            Intent intent = new Intent(HomeActivity.this,MainActivity.class);
            startActivity(intent);



        }
    });




}
}

最佳答案

如果您希望每次用户离开应用程序时都进行注销,那么为什么不手动进行注销呢?

您已经设置了注销按钮setOnClickListener,因此只需执行以下操作:

@Override
protected void onPause() {
    super.onPause();
    if(Logout != null) {
        Logout.callOnClick();
    }
}


这意味着用户每次离开此活动都会注销。
确保您处理的情况是:如果他们离开活动但转到页面上的其他活动,则该活动不会注销。

===========编辑===========

我猜您的login活动是应用程序打开时开始的主要活动,因此在执行任何其他任务之前,只需将其放在onCreate中即可。在setContentView之后,您应该执行此操作。

SharedPreferences sharedPreferences = getSharedPreferences(login.MyPREFERENCE, Context.MODE_PRIVATE);

String phone = sharedPreferences.getString(PHONE, "");
String pass = sharedPreferences.getString(PASS, "");

if(!phone.isEmpty() && !pass.isEmpty()) {
    // this means ID and passwords are already saved so just start your home activity here
    startActivity(new Intent(context, MainActivity.class));
    finish();
}


在您的MainActivity中:

只需读取SharedPreferences中的值并根据需要登录。

希望这可以帮助。

==========编辑2

另外,您的注销应该看起来像这样。不要使用commmitclear。放入空值并使用apply

Logout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        SharedPreferences sharedPreferences = getSharedPreferences(login.MyPREFERENCE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(PHONE, "");
        editor.putString(PASS, "");
        editor.apply();
        Intent intent = new Intent(HomeActivity.this,MainActivity.class);
        startActivity(intent);
    }
});

08-04 00:05