简体   繁体   English

使用标志Intent.FLAG_ACTIVITY_NEW_TASK从onPostExecute()方法启动意图不起作用

[英]Starting intent from onPostExecute() method with a flag Intent.FLAG_ACTIVITY_NEW_TASK is not working

I am using the default LoginActivity , when you go to New... > Android Activity inside Eclipse with ADT Tools on Android 4.2 SDK. 当我使用Android 4.2 SDK上的ADT Tools转到Eclipse中的New ...> Android Activity时,我正在使用默认的LoginActivity

It contains an email field, password field and a button that says "Sign in or Register". 它包含一个电子邮件字段,密码字段和一个“登录或注册”按钮。 When you click on that button, it performs a fake async network request, calls a few methods to show progress bar, verify user login with dummy data, and the last thing that gets called if everything is successful: 当您单击该按钮时,它会执行虚假的异步网络请求,调用几种方法来显示进度条,使用虚拟数据验证用户登录,以及如果一切都成功则调用的最后一项:

        @Override
        protected void onPostExecute(final Boolean success) {
            mAuthTask = null;
            showProgress(false);

            if (success) {
                //finish();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
                startActivity(intent);
            } else {
                mPasswordView
                        .setError(getString(R.string.error_incorrect_password));
                mPasswordView.requestFocus();
            }


}

I commented out finish(), and instead would like to open a new activity window that would be my main app window (Logged in state). 我注释掉了finish(),而是希望打开一个新的活动窗口,它将成为我的主应用程序窗口(登录状态)。 I just started with Android yesterday, and I've seen a lot of examples use fragments for this sort of thing instead of activities. 我昨天刚刚开始使用Android,我看到很多例子都使用片段来代替活动。 Does it matter? 有关系吗? Anyway, it works fine, it opens a new activity but I can still press "Back button" and it will take me the login screen . 无论如何,它工作正常,它打开一个新的活动,但我仍然可以按“后退按钮”,它将带我登录屏幕 I am pretty sure those flags I set below on the activity is what's required to clear the history stack, but it doesn't work. 我很确定我在活动下面设置的那些标志是清除历史堆栈所需的,但它不起作用。

How can I fix above code to clear the history stack? 如何修复上面的代码以清除历史堆栈? In other words after I log in, it should take me to the new activity, and if I press Back , it should take me to the Home screen . 换句话说,在我登录后,它应该带我进入新活动,如果我按Back ,它应该带我进入主屏幕

Update 更新

I have attached code for LoginActivity, MainActivity, AndroidManifest. 我附加了LoginActivity,MainActivity,AndroidManifest的代码。

Link : https://gist.github.com/sahat/5445657 链接https//gist.github.com/sahat/5445657

Use Intent.FLAG_ACTIVITY_CLEAR_TOP instead. 请改用Intent.FLAG_ACTIVITY_CLEAR_TOP The flags you are using create a new task but don't do anything to the rest of the stack. 您正在使用的标志创建一个新任务,但不对堆栈的其余部分执行任何操作。 This flag will clear all other Activities . 此标志将清除所有其他Activities If the Activity exists, it will just clear out the rest of the Activities so the MainActivity will be on top 如果Activity存在,它将清除其余Activities以便MainActivity位于顶部

FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_CLEAR_TOP

Edit 编辑

If you want to go to the Home screen after pressing back from the next Activity then just call finish() on the login Activity when you log in. This will remove the login page from your stack. 如果您想在从下一个Activity返回后返回主屏幕,则只需在登录时调用登录Activity上的finish() 。这将从您的堆栈中删除登录页面。 You can also specify 你也可以指定

android:noHistory="true"

in the Activity tag for login screen in manifest manifest登录屏幕的Activity标签中

Try this 尝试这个

@Override
    protected void onPostExecute(final Boolean success) {
        mAuthTask = null;
        showProgress(false);

        if (success) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class)
            startActivity(intent);
            finish();
        } else {
            mPasswordView
                    .setError(getString(R.string.error_incorrect_password));
            mPasswordView.requestFocus();
        }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM