Override Material Design activity return transition

786 Views Asked by At

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.

I have a login screen as my first activity, in which I've set up an exit transition in the onCreate method:

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. There may be a better way of doing this, but my Google searches so far have not come up with anything better.

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:

Undesirable app close result

As you can see, the shared toolbar has not disappeared, the FAB has not disappeared, and the RecyclerView has faded the background to transparent. 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?

1

There are 1 best solutions below

7
On

Try setting the following flags to your 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());