How avoid returning to login layout pressing the back button/key?

落花浮王杯 提交于 2019-12-04 03:23:07
Enrique Marcos

I've implemented something similiar using SharedPreferences. I did this:

LoginActivity

SharedPreferences settings;
public void onCreate(Bundle b) {
    super.onCreate(b);
    settings = getSharedPreferences("mySharedPref", 0);
    if (settings.getBoolean("connected", false)) {
        /* The user has already login, so start the dashboard */
        startActivity(new Intent(getApplicationContext(), DashBoardActivity.class));
    }
    /* Put here the login UI */
 }
 ...
 public void doLogin() {
    /* ... check credentials and another stuff ... */
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("connected", true);
    editor.commit();
 }

In your DashBoardActivity override the onBackPressed method. This will take you from DashBoardActivity to your home screen.

@Override
public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}  

Hope it helps.

One idea is to initially launch the dashboard and then launch the login over it in a new Activity if you detect that the user isn't logged in. You can then skip past the login dialog as needed. If you set noHistory="true" on the login Activity in your manifest, it will be prevented from reappearing on back pressed.

Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.

@Override
public void onBackPressed() {
    moveTaskToBack(true);
    super.onBackPressed();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!