4

我有两个活动,我通过简单的来回滑动在它们之间进行转换。使用以下代码在第一次使用 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);
          }

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

4

2 回答 2

4

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

前段时间,我在应用程序中创建了一个向导样式部分,用户需要通过几个步骤前进或后退来完成向导。每一步都是一个Activity,并且应用程序在每一步中都会收集信息,或者用户可以返回任何步骤以更正某些内容,当他返回上一步时,这一步的用户信息应该仍然存在,避免用户再次填充一些东西。

我没有滑动,而是在每个步骤中使用两个按钮:前进和后退。但是您的情况与此相同,因为无论使用滑动还是按钮,您都希望随时通过动画转换来更改活动。

对于随时使用过渡,您不能一直保持活动活跃。正如文档所说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);
}

如果你看到nextButtonOnClickand onBackPressed,每次我使用overridePendingTransition,我finish之前都使用了一行。这让我确保这个活动在离开时会被杀死,所以我的动画转换将始终被执行。

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

最后,如果用户离开这一步然后他又回来了,onCreate我们尝试从 Intent 的Bundle(如果有的话)中取回信息restoreRegistrationValues

我希望它对你有帮助。

更新

该解决方案符合Activity生命周期,这意味着Activity娱乐是一个完全自然的过程。Activity您正在使用Activity提供的相同工具,Intent以及Bundle重新创建Activity. 此外,您应该考虑如果您的活动长时间未使用,它们可能会被破坏。正如文件所说:

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

同样的,即使你Activity被旋转了,也被销毁了,你需要保存和恢复它的值,然后重新创建一个新的Activity.

如果您愿意,如果您对此解决方案仍然感到不舒服,请花点时间了解更多信息。

于 2013-07-08T22:33:53.993 回答
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);
    }
于 2013-09-13T20:08:56.803 回答