android - 当我在登录页面上按下后退按钮时,它将转到主菜单(在我选择是后在 MainMenu Activity 中注销)

标签 android

这是我的 MainMenu.java 的一部分

public class MainMenu extends Activity{

Button userinfo,requestservice,makepayment,trackparcel,checkcard;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    // Show the Up button in the action bar.
    setupActionBar();
    userinfo = (Button) findViewById(R.id.userinfo);
    requestservice = (Button) findViewById(R.id.requestservice);
    makepayment = (Button) findViewById(R.id.makepayment);
    trackparcel = (Button) findViewById(R.id.trackparcel);
    checkcard = (Button) findViewById(R.id.checkcard);

    userinfo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, UserInfo.class);              
            startActivity(intent);
        }
    });
    requestservice.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, RequestService.class);
            startActivity(intent);

        }
    });
    makepayment.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, Payment.class);
            startActivity(intent);

        }
    });
    trackparcel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, TrackParcel.class);
            startActivity(intent);
        }
    });
    checkcard.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainMenu.this, CheckCard.class);
            startActivity(intent);
        }
    });

}

public void onBackPressed() {
    new AlertDialog.Builder(MainMenu.this).setTitle("Logout")
    .setMessage("Would you like to logout?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            Intent intent = new Intent(MainMenu.this, Login.class);
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(MainMenu.this);
            Editor edit = sp.edit();
            edit.clear();
            edit.commit();
            startActivity(intent);
        }
     })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // user doesn't want to logout
        }
     })
    .show();
}

这是我的 Login.java 的一部分

public class Login extends ActionBarActivity implements OnClickListener {

private Button login, register;
private EditText email, password;

JSONArray loginposition = null;
// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

private static final String LOGIN_URL = "http://XXX.XXX.X.XX:1234/PMSS/login.php";
// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_POSTS = "posts";
private static final String TAG_EMAIL = "email";
private static final String TAG_POSITION = "position";

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    login = (Button) findViewById(R.id.login);
    register = (Button) findViewById(R.id.registerlauncher);
    email = (EditText) findViewById(R.id.userid);
    password = (EditText) findViewById(R.id.password);

    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String Username = email.getText().toString();
            String Password = password.getText().toString();
            new AttemptLogin(Username, Password).execute();
        }
    });

    register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Login.this, Register.class);
            startActivity(intent);

        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // For the main activity, make sure the app icon in the action bar
        // does not behave as a button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

public void onBackPressed() {
    Intent intent = new Intent(this, SplashScreen.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);
            finish();
}

我只是不知道为什么在 MainMenu.java 中选择"is"注销后,它会返回到我想要的登录页面,但是一旦我到达登录页面,我又按回车键它将返回主菜单而没有任何 logcat..

最佳答案

我认为您需要在MainMenuonBackPressed 方法中调用finish。这将从返回堆栈中删除 Activity 。

public void onBackPressed() {
    new AlertDialog.Builder(MainMenu.this).setTitle("Logout")
    .setMessage("Would you like to logout?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            Intent intent = new Intent(MainMenu.this, Login.class);
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(MainMenu.this);
            Editor edit = sp.edit();
            edit.clear();
            edit.commit();
            startActivity(intent);

            finish();  // Call finish here.
        }
     })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // user doesn't want to logout
        }
     })
    .show();
}

关于android - 当我在登录页面上按下后退按钮时,它将转到主菜单(在我选择是后在 MainMenu Activity 中注销),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20586925/

相关文章:

android - RxJava2。对列表中的每个项目执行请求

android - 如何在 Android 中列出所有帐户(Facebook、Twitter、Gmail 等)?

java - 如何处理 LocalDate period. Between 结果?

android - gradle 属性为 "experimental and unsupported"意味着什么?

android - 如何完全禁用android上的菜单按钮?

java - sqlite数据库帮助

java - 链式动画不适用于 AnimatorSet

android - 如何在运行时获取准确的ListView高度?

android - 如何测量 Android 上的移动数据流量

java - 屏幕旋转后恢复 fragment 中微调器的位置