I'm using the Google DrawerLayout
.
When an item gets clicked, the drawer is smoothly closed and an Activity
will be launched. Turning these activities into Fragment
s is not an option. Because of this, launching an activity and then closing the drawer is also not an option. Closing the drawer and launching the activity at the same time will make the closing animation stutter.
Given that I want to smoothly close it first, and then launch the activity, I have a problem with the latency between when a user clicks on the drawer item, and when they see the activity they wanted to go to.
This is what the click listener for each item looks like.
final View.OnClickListener mainItemClickListener = new View.OnClickListener() {
@Override
public void onClick(final View v) {
mViewToLaunch = v;
mDrawerLayout.closeDrawers();
}
};
My activity is also the DrawerListener, its onDrawerClosed
method looks like:
@Override
public synchronized void onDrawerClosed(final View view) {
if (mViewToLaunch != null) {
onDrawerItemSelection(mViewToLaunch);
mViewToLaunch = null;
}
}
onDrawerItemSelection
just launches one of the five activities.
I do nothing on onPause
of the DrawerActivity
.
I am instrumenting this and it takes on average from 500-650ms from the moment onClick is called, to the moment onDrawerClosed ends.
There is a noticeable lag, once the drawer closes, before the corresponding activity is launched.
I realize a couple of things are happening:
The closing animation takes place, which is a couple of milliseconds right there (let's say 300).
Then there's probably some latency between the drawer visually closing and its listener getting fired. I'm trying to figure out exactly how much of this is happening by looking at
DrawerLayout
source but haven't figured it out yet.Then there's the amount of time it takes for the launched activity to perform its startup lifecycle methods up to, and including,
onResume
. I have not instrumented this yet but I estimate about 200-300ms.
This seems like a problem where going down the wrong path would be quite costly so I want to make sure I fully understand it.
One solution is just to skip the closing animation but I was hoping to keep it around.
How can I decrease my transition time as much as possible?
A better approach would be to use the onDrawerSlide(View, float) method and start the Activity once the slideOffset is 0. See below
Just set the mPendingDrawerIntent in the Drawer's ListView.OnItemClickListener onItemClick method.
So I seem to have solved the problem with a reasonable solution.
The largest source of perceivable latency was the delay between when the drawer was visually closed, and when
onDrawerClosed
was called. I solved this by posting aRunnable
to a privateHandler
that launches the intended activity at some specified delay. This delay is chosen to correspond with the drawer closing.I tried to do the launching
onDrawerSlide
after 80% progress, but this has two problems. The first was that it stuttered. The second was that if you increased the percentage to 90% or 95% the likelihood that it wouldn't get called at all due to the nature of the animation increased--and you then had to fall back toonDrawerClosed
, which defeats the purpose.This solution has the possibility to stutter, specially on older phones, but the likelihood can be reduced to 0 simply by increasing the delay high enough. I thought 250ms was a reasonable balance between stutter and latency.
The relevant portions of the code look like this:
I was facing same issue with DrawerLayout.
I have research for that and then find one nice solution for it.
What i am doing is.....
If you refer Android Sample app for the DrawerLayout then check the code for selectItem(position);
In this function based on the position selection fragment is called. I have modify it with below code as per my need and works fine with no animation close stutter.
Here i am first closing the DrawerLayout. which takes approx 250 miliseconds. and then my handler will call the fragment. Which works smooth and as per the requirement.
Hope it will also helpful to you.
Enjoy Coding... :)
According the docs,
Instead of using a
Handler
and hard-coding the time delay, you can override theonDrawerStateChanged
method ofActionBarDrawerToggle
(which implementsDrawerLayout.DrawerListener
), so that you can perform the expensive operations when the drawer is fully closed.Inside MainActivity,
Set the
DrawerListener
inonCreate
:Finally,
I'm using approach like below. Works smoothly.
The same with ActionBarDrawerToggle:
Google IOsched 2015 runs extremely smoothly (except from settings) the reason for that is how they've implemented the drawer and how they launch stuff.
First of they use handler to launch delayed:
with delay being:
Another thing they do is to remove animations from the activities that are launched with following code inside the activities onCreate():
To view the source go to git.