简体   繁体   English

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

[英]My Android transition animation is only working once

I have two activities and I transition between the two of them by doing a simple slide back and forth. 我有两个活动,我通过简单的来回滑动在两个活动之间进行过渡。 With the following code that works just fine on the first time by using overridePendingTransition but that method doesn't work after an activity is already created and I keep these two alive the whole time the app is active. 使用以下代码通过使用overridePendingTransition第一次正常工作,但该方法在已创建活动后不起作用,并且在应用程序处于活动状态时我保持这两个活动。 Is there a method that I can put in here that works even after activities are created and alive? 有没有一种方法可以放在这里,即使在活动创建和活动之后也可以使用?

//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);
          }

My animation works correctly and simply has it hold the current activity where it is and brings the new one in from the left. 我的动画工作正常,只是让它保持当前活动的位置,并从左侧开始新的动画。

As you have seen, overridePendingTransition only works if you going to show a new activity (or finish an old one). 如您所见, overridePendingTransition仅在您要显示新活动(或完成旧活动)时才有效。

Some time ago, I created a wizard style section in an app where user needs go forward or backward trough several steps to complete the wizard. 前段时间,我在应用程序中创建了一个向导样式部分,用户需要前进或后退几个步骤才能完成向导。 Each step is an Activity , and the app recollects information in every forward step, or user can go back any steps to correct something and when he returns to the last step he was, the user information of this step should be still there, avoiding the user to fill something again. 每个步骤都是一个Activity ,应用程序会在每个前进步骤中重新收集信息,或者用户可以返回任何步骤来更正某些内容,当他返回到最后一步时,此步骤的用户信息应该仍然存在,避免用户再次填写一些东西。

Instead of swipe, I use two buttons in each step: go and back. 我没有滑动,而是在每一步中使用两个按钮:往返。 But your case is the same than this one, because no matter if using swipe or buttons, you want to change your activities with an animation transition anytime. 但是你的情况与此相同,因为无论使用滑动还是按钮,你都希望随时通过动画转换来改变你的活动。

For using transition anytime, you cannot keep your activities alive all the time. 对于任何时候使用转换,您无法始终保持活动。 As documentation says about overridePendingTransition : 关于overridePendingTransition 文档说:

Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next. 在startActivity(Intent)或finish()的一种风格之后立即调用,以指定下一步要执行的显式转换动画。

The thing you should do is keep the information taken in every activity saved, kill the activity, create a new one, and bring the information back again to restore the new activity. 您应该做的是保存在保存的每个活动中获取的信息,终止活动,创建新活动,然后再次返回信息以恢复新活动。

To save information, you can use the same Intent you are using to create new activities. 要保存信息,您可以使用与创建新活动相同的Intent It has a Bundle where you can be depositing the information. 它有一个Bundle ,您可以存放信息。

For example: 例如:

@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);
}

If you see in nextButtonOnClick and onBackPressed , every time I use overridePendingTransition , I use finish one line before. 如果你在nextButtonOnClickonBackPressed看到,每次我使用overridePendingTransition ,我都会在之前使用finish一行。 This make me sure this activity will be killed when leaving, so my animation transition will be always executed. 这使我确保这个活动在离开时会被杀死,所以我的动画过渡总会被执行。

Then I save my information, in this Activity the app asks the user for a username and password. 然后我保存我的信息,在此Activity ,应用程序要求用户输入用户名和密码。 So, before leaving, we save that introduced by the user in our Intent's Bundle . 因此,在离开之前,我们将用户在Intent的Bundle中引入的内容保存起来。

Finally, if the user leave this step and then he comes back, in onCreate we try to take the information back from the Intent's Bundle (if any) in restoreRegistrationValues . 最后,如果用户离开此步骤然后他回来,在onCreate我们尝试从restoreRegistrationValues的Intent的Bundle (如果有的话)中获取信息。

I hope it helps you. 我希望它对你有所帮助。

UPDATE UPDATE

This solution complies with the Activity lifecycle, which means Activity recreation is a process completely natural. 此解决方案符合Activity生命周期,这意味着Activity娱乐是一个完全自然的过程。 You are using the same tools provided by the Activity , such as Activity's Intent and Bundle for recreating your Activity . 您正在使用Activity提供的相同工具,例如Activity的IntentBundle来重新创建Activity Furthermore, you should consider that your activities may be destroyed if they has not been used for a long time. 此外,您应该考虑如果您的活动长时间未使用,可能会被销毁。 As documentation says: 正如文件所说:

The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory. 如果系统当前已停止且未长时间使用或前台活动需要更多资源,系统也可能会破坏您的活动,因此系统必须关闭后台进程才能恢复内存。

In the same way, even when your Activity is rotated, is also destroyed, and you will need to save and restore its values, and recreate a new Activity . 同样,即使您的Activity被旋转,也会被销毁,您需要保存并恢复其值,并重新创建一个新的Activity

Please, if you want, take your time to find out more if you feel still uncomfortable with this solution. 如果您愿意,请花点时间了解如果您对此解决方案感到不舒服的话。

I have an even simpler solution! 我有一个更简单的解决方案! It worked awesome for me! 它对我来说真棒! What I did is that I used the if condition to tell it whether the overridePendingTransition onPause(); 我做的是我使用if条件告诉它是否overridePendingTransition onPause(); was true or false. 是真还是假。 Type this: 输入:

    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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM