Android Lollipop home button return animation - android

I'm trying the new Android API, specifically the new animations. I have two activities and used setEnterTransition() and setExitTransition() on the second activity with a Slide transition. Everything works fine when I switch activities using the buttons inside them, or using the back button, but when I'm on the second activity and I press the Home Button the return animation is not played...
Second activity onCreate() method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getActionBar() != null) {
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Slide slideTransition = new Slide();
slideTransition.setDuration(1000);
slideTransition.setSlideEdge(Gravity.RIGHT);
getWindow().setEnterTransition(getSlideTransition(Gravity.RIGHT, 1000));
getWindow().setExitTransition(getSlideTransition(Gravity.LEFT, 1000));
getWindow().setAllowEnterTransitionOverlap(true);
setContentView(R.layout.activity_second);
//...
}
The first activity is set as parent of the second activity in the manifest.
I find some solutions that use the overridePendingTransition() method, but I would like a solution that uses the new methods (if that is possible).
Thanks!

This is by design. The return transition is only triggered when the activity is explicitly finished (i.e. you press the back button or call finishAfterTransition()). When you press the home button, you are putting the application into the background so that the user can return to that same activity at a later time. If you were to finish the activity when you pressed the home button, the user would be confused as to why they were not taken to that same activity when they return to the application later on.
It is also worth mentioning that the new Lollipop transition APIs are not meant to replace overridePendingTransition(). The two are fundamentally different. The Lollipop transition APIs give you a way to animate the contents inside an activity's view hierarchy individually when you switch from one activity to another. On the other hand, overridePendingTranition() allows you to override the system's default window animation when the activity window is being added or removed from the screen. In other words, the former operates on the views inside the activity's window whereas the latter operates on the entire activity window itself.
One last major difference between the two is that the new Lollipop transition APIs only work between two activities that belong to the same task. If you want to perform an exit/enter animation when navigating between two activities belonging to two different tasks, you'd need to use overridePendingTransition() instead.

Related

Activity Lifecyle Difference in Device's Back Button vs Actionbar's Back Button

I'm currently learning the Activity Lifecyle. I noticed the following:
I have two Activitys, A and B.
When I open Activity B from Activity A, A gets stopped and B gets created and started.
When I press the Back Button on my device, B gets destroyed and A get restarted.
But when I use the Back / Up Botton of the Actionbar instead, B gets destroyed, A gets destroyed and then onCreate() is called.
Why gets A destroyed instead of restarted, when using the Up Botton in the ActionBar?
I hope my question is clear, if not please comment.
When you press the BACK button, this calls onBackPressed() in the current Activity. The default behaviour of that method (if not overridden in the Activity) is to call finish() on the Activity. This finishes the Activity and resumes the Activity which is underneath it.
The UP button is calling startActivity() with an Intent that is built like this:
Intent intent = new Intent(this, TargetActivityForUpButton.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This code will remove all activities in the stack back to, and including, TargetActivityForUpButton. It then creates a new instance of TargetActivityForUpButton and launches that Actvity (you will see onCreate(), onStart(), onResume() called on the Activity.
See also the section "Navigate up to parent activity" in https://developer.android.com/training/implementing-navigation/ancestral
The device's back button is actually taking you back (to the previous activity). The action bar back button works similarly to an "Up" button (within the hierarchy of your app). This is why the action bar's back button won't take you outside of the app, whereas the device's back button will carry on taking you back, even outside of the app. The action bar exists within your app so it follows the activity's lifecycle methods and starts from scratch each time you go back, whereas the device is restarting from where it stopped.
EDIT:
The Back button appears in the system navigation bar and is used to navigate, in reverse chronological order, through the history of screens the user has recently worked with. It is generally based on the temporal relationships between screens, rather than the app's hierarchy.
(Read more here)

How to prevent recreation of a singleTask activity on back button press?

I have a simple launcher app, that doesn't launch any apps, just shows a WebView. It is set as the default launcher. I don't use any other apps on this tablet. When I restart the tablet, this launcher starts as expected. My problem is if I press the back button of the device, the activity is destroyed, and it is created again as a new activity. I don't get what's the point of this behaviour, as the activity is already visible... The view's launch mode is singleTask, so I expect it to just simply stay where it is, rather than being recreated. How can this be done? As far as I understand the documentation, this should work as I expect it, instead of the way it does now.
The system creates the activity at the root of a new task and routes
the intent to it. However, if an instance of the activity already
exists, the system routes the intent to existing instance through a
call to its onNewIntent() method, rather than creating a new one.
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
You can override onBackPressed() and do nothing like
#Override
public void onBackPressed() { }

Lollipop transitions not working when resuming activity

I have been trying to implement the new Lollipop activity and shared elements transitions during a few days following the steps from Alex Lockwood awesome blog posts.
But now I'm facing a bit of a problem.
My application uses a DrawerLayout for navigation, but I can also launch some of the activities when clicking other views and buttons. I have set correctly all the Enter, Reenter, Return and Exit transitions for all the activities as well as calling:
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity, getSharedViewsPairs(activity)).toBundle());
for the shared elements between those activites.
If I launch the activities for the first time, the transitions work up well. My problem comes when I try to launch/resume an activity that has already been called before but that now is paused and lives still on the activity stack. When I try to bring this activity to the foreground, then there is no transition.
I also have to say that I set to all my activities the Intent flag FLAG_ACTIVITY_REORDER_TO_FRONT so that in case they have been already launched before, I don't fresh launch them again. Could this have anything to do with it? Am I missing some method that needs to be called when resuming activities that have been launched previously with transitions?
Code for preparing the activity transitions:
public static void requestTransitionsAnimations(Activity activity) {
if(MaterialAnimations.isAnimationSupported()){
Transition transition = TransitionInflater.from(activity).inflateTransition(R.transition.material_transitions);
activity.getWindow().requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
activity.getWindow().setAllowEnterTransitionOverlap(true);
activity.getWindow().setAllowReturnTransitionOverlap(true);
activity.getWindow().setEnterTransition(new Explode());
activity.getWindow().setReenterTransition(new Explode());
activity.getWindow().setReturnTransition(new Explode());
activity.getWindow().setExitTransition(new Explode());
activity.getWindow().setSharedElementsUseOverlay(true);
activity.getWindow().setSharedElementEnterTransition(transition);
activity.getWindow().setSharedElementReenterTransition(transition);
activity.getWindow().setSharedElementExitTransition(transition);
activity.getWindow().setSharedElementReturnTransition(transition);
}
}
as

Is it normal to call two activities from two buttons in sequence

I didn't know how to phrase the question properly so let me explain.
I have noticed that on the android platform that when you press two buttons in one activity in quick sequence (press one button then the other before the activity has a chance to leave the screen) that two activities are called one after another. It is not visible while it happens but if you press the back button then the activity that was called with the second button leaves (finishes) and the activity that was called with the first button shows up. You have to go back again to go back to the calling Activity. So you have to press back twice to get to Activity 2's parent activity.
So I want to know if this is a problem for others and if so how would you go about fixing that. Or do you think this is not much of a problem.
You can call finish() after startActivity(your_intent); that way the activity you are leaving finishes and you get to the next one, so you dont have to press back twice to get to it's parent Activity.
I have figured out how to pass this problem. I have a base activity class that every other activity in my application inherits from. I override
onResume with:
#Override
protected void onResume()
{
super.onResume();
setCanStartNewActivity(true);
}
and startActivityForResult with:
#Override
public void startActivityForResult(Intent intent, int requestCode)
{
//Does not allow starting a new activity unless previous activity returned
//This is a trick to stop multiple simultaneous button presses starting multiple
//activities simultaneously.
if(!canStartNewActivity)
return;
setCanStartNewActivity(false);
super.startActivityForResult(intent, requestCode);
}
This basically makes sure only one new activity can be started for a button press. If you let go of two buttons at the same time only the button released the earliest (even by 10 microseconds) will be fired and the other will still send the intent but it will not fire until the first fired activity returns or the app dies.

android - How to Exit from the application

My Application have 20 activities. Here i want to implement the how to exit from the application when you click on the button(like Logout). it means if you click on the menu button any where of our application then it shows the one button. if click on that then directly comes out from the application. how to implement it.
thanks
Well naresh you can do something like that
first finish the activity from which you are closing application this.finish(); secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this clear top when you switch from one activity to another activity and as you know every activity is kept in stack to so this flag remove old activity from top and push new activity in top so around your whole application only one activity is kept in stack
and if this does not work put the whole application in background by avtivityname.moveTaskToBack(); this will move your whole app in back ground but only one drawback when you start your activity it will show your that activity from which you have moved back
System.exit(0);
should work, don't forget Java common functions work on android, there isn't only the android library!
As for the button being in the menu in every activity, you could create a class derived from Activity which creates and handles the menu properly, and make every other activity inherit that derived activity.
First finish the activity from which you are closing application: this.finish();. Secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); This clears top when you switch from one activity to another activity. As you know every activity is kept in a stack to so this flag removes old activity from the top and pushes new activity to the top so around your whole application only one activity is kept in the stack.
If this does not work, put the whole application in background with avtivityname.moveTaskToBack();. This will move your whole app to the background. One drawback: when you start your activity it will show your that activity from which you have moved back.

Categories

Resources