Android:使用navigateUpFromSameTask()时的错误转换

Lin*_*lda 11 android android-navigation

我刚刚开始使用Android,我按照本指南将向上导航添加到操作栏上的活动.

http://developer.android.com/training/implementing-navigation/ancestral.html

然而,当我使用动作栏中的向上按钮时,它的转换使其显示为打开新活动的淡入过渡,而不是它应该的淡出,就像按下按钮上的后退按钮时一样.底部.

我该怎么做才能让它显示正确的过渡?谢谢.

小智 16

android:launchMode="singleTop"在您的Manifest.xml中为您的父Activity 声明.

在调用类似以下内容时,转换将更改为默认淡出:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)

(来自:http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp)


Cip*_*ian 11

你应该使用navigateUpTo()方法并将标志没有动画设置为意图,如下所示:

final Intent intent = NavUtils.getParentActivityIntent(this);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
NavUtils.navigateUpTo(this, intent);
Run Code Online (Sandbox Code Playgroud)

你将拥有'漂亮'的背景动画,以及正确使用向上按钮.this是当前的活动.


Lin*_*lda 0

我把它改成finish(); 这似乎是我想要的。

  • 是的,但这是不正确的,请查看此处的演示文稿,为什么不建议这样做:https://speakerdeck.com/jgilfelt/this-way-up-implementing- effective-navigation-on-android (5认同)