按下后退按钮相同 activity 再次重新加载

On pressing back button same activity reloads again

这里发生的事情是,一旦按下后退按钮 activity 会再次重新加载,再次按下后退按钮会退出应用程序。我希望它只出现一次,即第一次

我尝试了很多方法,例如试验 onBackPressed 方法,但 none 方法奏效了。我想解决这个问题,当用户按下后退按钮时,他应该退出应用程序。 其他 activity 也发生了同样的情况,其中我有一个登录页面,一旦用户进入登录页面,如果他想退出应用程序并按下后退按钮,那么在我的情况下登录页面又是重新加载,然后在再次按下后退按钮后退出应用程序。

下面是代码。

public class MainActivity extends AppCompatActivity {
    CardView attendance, time_table, directory, daily_work, notices, transport,remarks,calendar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences preferences = getSharedPreferences("user_login",MODE_PRIVATE);
        boolean isLogin = preferences.getBoolean("isLogin", false);
        if(isLogin)
        {
            init();
            methodListener();
        }
        else {
            Intent intent = new Intent(this, Login.class);
            startActivity(intent);
            finish();
        }


    }

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }


public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.logout) {
            SharedPreferences preferences = getSharedPreferences("user_login", MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            editor.clear();
            editor.commit();
             Intent intent = new Intent(this,Login.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}

请提出一些修复方法。

像下面那样使用 onBackPressed()

    @Override
public void onBackPressed() {
    AlertDialog.Builder builder1 = new AlertDialog.Builder(HomeActivity.this);
    builder1.setTitle("Close...?");
    builder1.setMessage("Do you want to exit?");
    builder1.setCancelable(true);
    builder1.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // Home.this.finish();
                    finishAffinity();
                }
            });
    builder1.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    AlertDialog alert11 = builder1.create();
    alert11.show();
}

你可以试试这个。这对我有用。

 @Override
public void onBackPressed() {
    new AlertDialog.Builder(this).setMessage("Are you sure you want to exit?").setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    }).create().show();
}

实现onBackPressed()如下:

 @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, Login.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

和onOptionsItemSelected(MenuItem item)如下:

public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.logout) {
         onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }