0

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?

Gareth
  • 5,693
  • 3
  • 28
  • 37
  • you can use (onBackPressed()) -- http://stackoverflow.com/questions/17719634/how-to-exit-an-android-app-using-code – Tasos Feb 20 '16 at 15:36
  • @Tasos Thanks, that's done the trick. Just added `finish()` to the `onBackPressed()` method. Care to make it into a proper answer so I can accept? – Gareth Feb 20 '16 at 20:10
  • give a point to the person from the link – Tasos Feb 21 '16 at 07:43

1 Answers1

0

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());
Elvis Chweya
  • 1,532
  • 11
  • 19
  • Sorry, this doesn't work. This actually breaks the transition from login to main, with the login activity visibly closing as the main activity slides in from the right. – Gareth Feb 20 '16 at 19:55
  • @Gareth, actually delete the `Handler` code for a delay. See the edit. Try and inform me what happens – Elvis Chweya Feb 20 '16 at 19:57
  • Thanks, but that makes no difference. The previously working transition from login to main is now broken and the unwanted transition out of main when I press back is still happening. – Gareth Feb 20 '16 at 20:01
  • Sorry I couldn't help. – Elvis Chweya Feb 20 '16 at 20:29
  • No problem. A comment on the question has pointed me in the right direction. – Gareth Feb 20 '16 at 20:42
  • 2
    This can't work because `ActivityOptions.makeSceneTransitionAnimation` does not work with cross-task Activities. – George Mount Feb 21 '16 at 19:26
  • @GeorgeMount I've been looking for that bit of knowledge for some time now. Thanks for that. Is this documented anywhere? – mp501 Mar 20 '18 at 17:02