我的Android过渡动画只运行一次

时间:2013-07-03 18:16:36

标签: android animation xamarin.android

我有两个活动,我通过简单的来回滑动在两个活动之间转换。使用以下代码通过使用overridePendingTransition第一次正常工作,但该方法在已创建活动后不起作用,并且在应用程序处于活动状态时我保持这两个活动。有没有一种方法可以放在这里,即使在活动创建并活着之后也可以使用?

//checking if this is the correct activity to make sure I don't start up the same one
if (_thisActivity.GetType() == typeof(FeedActivity))
      {   
         this._baseActivity.OverridePendingTransition(Resource.Animation.LeftToRight, Resource.Animation.Hold);
      }
//checking if it is the other type
if (_thisActivity.GetType() == typeof(ListActivity))
          {   
             this._baseActivity.OverridePendingTransition(Resource.Animation.RightToLeft, Resource.Animation.Hold);
          }

我的动画工作正常,只是让它保持当前活动的位置,并从左侧开始新的动画。

2 个答案:

答案 0 :(得分:4)

如您所见,overridePendingTransition仅在您展示新活动(或完成旧活动)时才有效。

前段时间,我在应用中创建了一个向导样式部分,用户需要前进或后退几个步骤才能完成向导。每个步骤都是Activity,应用程序会在每个前进步骤中重新收集信息,或者用户可以返回任何步骤来更正某些内容,当他返回到最后一步时,此步骤的用户信息应该仍然是在那里,避免用户再次填充。

不是滑动,而是在每一步中使用两个按钮:往返。但是你的情况与此相同,因为无论是使用滑动还是按钮,你都希望随时通过动画转换来改变你的活动。

要随时使用转换,您无法始终保持活动。正如documentation所说overridePendingTransition

  

在startActivity(Intent)或finish()的一种风格之后立即调用,以指定下一步要执行的显式转换动画。

您应该做的是保存每个活动中保存的信息,终止活动,创建一个新活动,然后再将信息带回来恢复新活动。

要保存信息,您可以使用相同的Intent来创建新活动。它有一个Bundle,您可以存放信息。

例如:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_step_one_register_wizard);

    // Get the components of the content layout
    usernameEditText = (EditText)findViewById(R.id.usernameEditText);
    passwordEditText = (EditText)findViewById(R.id.passwordEditText);

    // Get the registration values which are in the extras of the current Intent (if any)
    restoreRegistrationValues();
}

/** Used to show the registration values which are in the extras of the current Intent */
private void restoreRegistrationValues() {
    Intent intent = this.getIntent();

    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        usernameEditText.setText(bundle.getCharSequence(Constants.KEY_USERNAME_TEXT));
        passwordEditText.setText(bundle.getCharSequence(Constants.KEY_PASSWORD_TEXT));
    }
}

/** Called when the user presses the Next button */
public void nextButtonOnClick(View view) {
    finish();
    overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right);

    Intent intent = this.getIntent();
    intent.setClass(this, StepTwoOneRegisterWizardActivity.class);
    intent.putExtra(Constants.KEY_USERNAME_TEXT, usernameEditText.getText());
    intent.putExtra(Constants.KEY_PASSWORD_TEXT, passwordEditText.getText());
    startActivity(intent);
}

/** Called when the user presses the Back button */
public void backButtonOnClick(View view) {
        onBackPressed();
}

@Override
/** Called when the user presses the Back hard button */
public void onBackPressed() {
    finish();

    overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
    Intent intent = this.getIntent();
    intent.setClass(this, StepZeroRegisterWizardActivity.class);
    intent.putExtra(Constants.KEY_USERNAME_TEXT, usernameEditText.getText());
    intent.putExtra(Constants.KEY_PASSWORD_TEXT, passwordEditText.getText());
    startActivity(intent);
}

如果您在nextButtonOnClickonBackPressed中看到,每次我使用overridePendingTransition时,我都会先使用finish一行。这使我确保在离开时会杀死此活动,因此我的动画过渡将始终执行。

然后我保存了我的信息,在此Activity应用程序要求用户输入用户名和密码。因此,在离开之前,我们将用户在Intent的Bundle中引入的内容保存起来。

最后,如果用户退出此步骤然后他回来,我会在onCreate中尝试从Bundle中的意图restoreRegistrationValues(如果有)中获取信息。< / p>

我希望它可以帮到你。

<强>更新

此解决方案符合Activity生命周期,这意味着Activity娱乐是一个完全自然的过程。您使用Activity提供的相同工具,例如活动的IntentBundle来重新创建Activity。此外,您应该考虑如果您的活动长时间未使用,可能会被销毁。正如documentation所说:

  

如果活动当前已停止且未长时间使用或前台活动需要更多资源,系统也可能会破坏您的活动,因此系统必须关闭后台进程才能恢复内存。

同样地,即使您的Activity被轮换,也会被销毁,您需要保存并恢复其值,然后重新创建新的Activity

如果您愿意,请详细说明如果您对此解决方案感到不安,请花点时间了解更多信息。

答案 1 :(得分:0)

我有一个更简单的解决方案! 它对我来说真棒! 我所做的是我使用if条件告诉它overridePendingTransition onPause();是真还是假。 输入:

    boolean anim = true; // make a boolean 'anim'
    ...
    public void backButtonOnClick(View view) {
        onBackPressed();
}

@Override
public void onBackPressed() {
            super.onPause();
            finish();
            if (anim) {
                overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left) //If boolean 'anim' is true, play these animations
            }
    ...
    public void nextButtonOnClick(View view) {
        finish();
    anim = false; //make 'anim' false so that it isn't used again
    overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right); //play these animations now
    startActivity(yourIntentToNextActivity);
    }