0

我正在为一个旧的 Android 应用程序编写一个新版本的工作,通过使用材料设计指南、图标和效果来更新它。

我有一个登录屏幕作为我的第一个活动,其中我在onCreate方法中设置了退出转换:

getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
super.onCreate(savedInstanceState);
getWindow().setExitTransition(exitTransition());

...

private Transition exitTransition()
{
    Fade fade=new Fade();
    fade.excludeTarget(android.R.id.statusBarBackground, true);
    fade.excludeTarget(android.R.id.navigationBarBackground, true);

    return fade;
}

...不包括状态栏和导航栏,以避免它们褪色为白色,然后变回原色。

我使用以下内容设置了主要活动:

getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
super.onCreate(savedInstanceState);
getWindow().setEnterTransition(enterTransition());

...

private Transition enterTransition()
{
    Slide slide=new Slide();
    slide.excludeTarget(android.R.id.statusBarBackground, true);
    slide.excludeTarget(android.R.id.navigationBarBackground, true);
    slide.setSlideEdge(Gravity.END);

    return slide;
}

然后我使用以下代码从登录活动启动主要活动:

Intent myIntent=new Intent(getApplicationContext(), MainActivity.class);
myIntent.putExtra("vehicles", sb.toString());
startActivity(myIntent, ActivityOptionsCompat.makeSceneTransitionAnimation(LoginActivity.this, toolbar, "toolbar").toBundle());
new Handler().postDelayed(new Runnable()
{
    @Override
    public void run()
    {
        //Finish the login activity so it can't be returned to.
        LoginActivity.this.finish();
    }
}, 1000);

我不希望后退按钮在用户完成后将他们带回登录页面,因为这并没有任何意义,所以我使用延迟完成来避免在使用时发生的noHistory闪烁清单。可能有更好的方法可以做到这一点,但到目前为止我的谷歌搜索还没有找到更好的方法。

这一切都如我所愿。问题是当我从主要活动中按下后退按钮时。应用程序不是以正常方式退出应用程序,而是尝试转换回现在完成的登录活动,给出以下内容:

不受欢迎的应用关闭结果

可以看到,共享工具栏并没有消失,FAB 也没有消失,RecyclerView 已经将背景淡化为透明。当过渡结束时,这一切都消失了。

那么-让应用程序知道登录活动现在已经消失并且从这里的后退按钮应该退出应用程序而不是尝试返回的正确方法是什么?

4

1 回答 1

0

尝试将以下标志设置为您的Intent

//...setup intent

//set CLEAR_TASK and NEW_TASK flags
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(myIntent, ActivityOptionsCompat.makeSceneTransitionAnimation(LoginActivity.this, toolbar, "toolbar").toBundle());
于 2016-02-20T15:40:01.953 回答