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.