开发者

Android - Slide Back to Home Screen

I've created a "back to home" type button in Android by using the code:

Intent i=new Intent(this, Home.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

However, when android transitions to the Home activity, it slides the activity in from right-to-left, giving the user the impression that a new activity was launched. The user then expects that when pressing "back", the previous activity would come up, which is obviously not the case.开发者_开发问答

How can I tell android to slide backwards (i.e. from left-to-right) so that the transition indeed gives the appearance of closing the previous activities?


You can override the animation by calling overridePendingTransition() after startActivity().

Look at this example from google.

Take a look at how to create an animation resource too.


Some more detail (copied from my answer to this question):

To specifically get the standard "back button" transition, I use these as the enterAnim and exitAnim values to overridePendingTransition(int enterAnim, int exitAnim):

push_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="@android:integer/config_shortAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="@android:integer/config_shortAnimTime" />
</set>

push_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="@android:integer/config_shortAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="@android:integer/config_shortAnimTime" />
</set>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜