4

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. 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.

devunwired
  • 62,780
  • 12
  • 127
  • 139
clifgray
  • 4,313
  • 11
  • 67
  • 116
  • If you're using API16 or newer, you should be able to accomplish this with ActivityOptions, since it seems OverridePendingTransition is only for newly-created activities. Where in the activity lifecycle do you have the above code? onCreate? – AWT Jul 08 '13 at 19:36
  • I have it so that it detects anytime in either of the two activities when a swipe is made and then it calls this function. Upon a swipe it creates the activity if need be and then goes through this if statement, and if one is already created it still goes through this if statement. – clifgray Jul 08 '13 at 19:58
  • And I am using API 10 and up – clifgray Jul 08 '13 at 19:59
  • This post maybe can helps [How can I repeat a transition forever?][1] [Or that ones][2] [2]: http://stackoverflow.com/questions/4480652/android-animation-does-not-repeat [1]: http://stackoverflow.com/questions/8582410/how-can-i-repeat-a-transition-forever – Alex Muni Jul 12 '13 at 10:57

2 Answers2

4

As you have seen, overridePendingTransition only works if you going to show a new activity (or finish an old one).

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.

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:

Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.

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. It has a Bundle where you can be depositing the information.

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. 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. So, before leaving, we save that introduced by the user in our Intent's 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.

I hope it helps you.

UPDATE

This solution complies with the Activity lifecycle, which means Activity recreation is a process completely natural. You are using the same tools provided by the Activity, such as Activity's Intent and Bundle for recreating your 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.

Please, if you want, take your time to find out more if you feel still uncomfortable with this solution.

Jorge Gil
  • 4,265
  • 5
  • 38
  • 57
  • Thanks that's very helpful. Is there really no way to keep them alive and do a transition between them, this is a good solution but it seems like there would be a much simpler way of doing it. It isn't really an option for me to make it this way but I may have to redesign the application. – clifgray Jul 09 '13 at 20:36
0

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(); 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);
    }
Paresh Kalinani
  • 93
  • 1
  • 11