2013-07-03 53 views
3

我有两个活动,我通过做一个简单的来回滑动,在两者之间转换。通过使用overridePendingTransition,第一次使用以下代码可以很好地工作,但是在一个活动已经创建之后,该方法不起作用,并且我保持这两个活动在整个应用程序活动期间保持活动状态。有没有一种方法可以放在这里,即使在活动创建并存活之后也可以工作?我的Android过渡动画只能工作一次

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

我的动画作品正确,只是有它保持当前的活动它在哪里,并从左边带来新的。

+0

如果您使用API​​16或更新版本,你应该能够ActivityOptions做到这一点,因为它似乎OverridePendingTransition仅适用于新创建的活动。在活动生命周期中,您是否拥有上述代码?的onCreate? – AWT

+0

我拥有它,以便它在两次活动中的任何一次检测到滑动时发生,然后它调用此函数。在刷卡时,它会根据需要创建活动,然后通过这个if语句,如果已经创建了它,它仍会经历这个if语句。 – clifgray

+0

我正在使用API​​ 10和以上 – clifgray

回答

4

正如你所看到的,overridePendingTransition只适用于如果你要显示一个新的活动(或完成一个旧的活动)。

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

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

对于随时使用转换,您不能始终保持活动状态。作为documentation说,大约overridePendingTransition:startActivity(意向)或完成的口味之一后

立即电话()来指定一个明确的过渡动画旁边的执行。

您应该做的事情是保存每个活动中保存的信息,杀死活动,创建一个新活动,然后再次返回信息以恢复新活动。

要保存信息,可以使用与创建新活动相同的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我们尝试从的Intent的Bundle(如果有)收回信息。

我希望它能帮助你。

UPDATE

该溶液符合的Activity生命周期,这意味着Activity娱乐是一个过程完全自然的。您正在使用Activity提供的相同工具,如Activity的IntentBundle用于重新创建您的Activity。此外,你应该考虑如果你的活动很长时间没有使用过,它们可能会被破坏。作为documentation说:

该系统还可以摧毁你的行为,如果它现在停止,并没有在很长一段时间被使用或前景活动需要更多的资源,因此系统必须关闭后台进程以恢复内存。

以同样的方式,甚至当你的Activity旋转时,也毁了,你将需要保存和恢复它的值,并重新创建一个新的Activity

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

+0

谢谢,这非常有帮助。是否真的没有办法让他们活着,并在他们之间进行转换,这是一个很好的解决方案,但似乎有一个更简单的方法来做到这一点。对我来说这不是一个真正的选择,但我可能不得不重新设计应用程序。 – clifgray

+0

我给你留下了更新。 –

+0

终于搞定了,谢谢! – clifgray

0

我有一个更简单的解决方案! 它为我工作真棒! 我所做的是我使用if条件来告诉它overridePendingTransitiononPause();是否为真或假。 键入:

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