简体   繁体   English

覆盖物料设计活动返回过渡

[英]Override Material Design activity return transition

I'm writing a new version of an old Android app for work, bringing it up to date by using the material design guidelines, icons and effects. 我正在编写旧版Android应用程序的新版本,使用材料设计指南,图标和效果将其更新。

I have a login screen as my first activity, in which I've set up an exit transition in the onCreate method: 我的第一个活动是登录屏幕,其中已在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;
}

...excluding the status bar and navigation bar to avoid them fading to white and then back to the primary dark colour. ...不包括状态栏和导航栏,以避免它们褪色为白色,然后变回原色。

I've set up the main activity using the following: 我使用以下方法设置了主要活动:

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;
}

I then launch the main activity from the login activity using this code: 然后,我使用以下代码从登录活动中启动主要活动:

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);

I don't want the back button to take the user back to the login page when they've finished as that doesn't really make any sense, so I'm using a delayed finish to avoid the flicker that happens if I use noHistory in the manifest. 我不希望后退按钮使用户在完成后返回登录页面,因为这实际上没有任何意义,所以我使用延迟完成来避免使用noHistory时发生的闪烁在清单中。 There may be a better way of doing this, but my Google searches so far have not come up with anything better. 也许有更好的方法,但是到目前为止,我的Google搜索还没有找到更好的方法。

This all works as I want it to. 这一切都按我的意愿进行。 The problem is when I push the back button from the main activity. 问题是当我从主要活动中按下后退按钮时。 Instead of exiting the app in the normal way, the app tries to transition back to the now-finished login activity giving this: 应用程序尝试以正常方式退出到现在完成的登录活动,而不是正常退出应用程序:

不良的应用关闭结果

As you can see, the shared toolbar has not disappeared, the FAB has not disappeared, and the RecyclerView has faded the background to transparent. 如您所见,共享工具栏没有消失,FAB也没有消失,并且RecyclerView使背景褪色为透明。 This all then just vanishes when the transition ends. 当过渡结束时,所有这些都将消失。

So - what is the correct way to let the app know that the login activity is now gone and the back button from here should just exit the app instead of trying to transition back? 那么-使应用程序知道登录活动现在已经消失并且从此处返回按钮应仅退出应用程序而不是尝试向后过渡的正确方法是什么?

Try setting the following flags to your Intent 尝试将以下标志设置为您的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());

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

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