如何避免按后退按钮/键返回登录布局?

Sol*_*orp 10 session android login back-button activity-stack

我想为我的学院创建一个应用程序.

问题是:我的应用程序将有两个布局(登录和仪表板).

学生可以正确填写登录表单,进入仪表板,按下按钮,填写其他字段.但是,如果用户然后按下后退按钮,它不应返回到登录屏幕,而是保留在仪表板中,否则退出应用程序.

然后,如果学生重新打开应用程序并且已经记录,则应自动将其重定向到仪表板,而不是登录屏幕,除非用户按下仪表板上的注销按钮,然后将其重定向回登录屏幕.

你怎么能做到这一点?

编辑: 我实现了2个意图和2个活动,并且我出现的新问题是,当我按下主页按钮并从任务管理器打开应用程序时,在剩下的活动中打开,但如果从图标打开则打开应用程序再次从第一个活动开始,在最后一个活动中打开了吗?

Enr*_*cos 5

我已经实现了类似的使用方法SharedPreferences.我这样做了:

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();
 }
Run Code Online (Sandbox Code Playgroud)

在你的DashBoardActivity覆盖onBackPressed方法中.这将带您DashBoardActivity到主屏幕.

@Override
public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}  
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.