1

I'm creating a Wizard-style application, where I need to keep the user data between activities A and B alive. A has a Next soft button and B has a Back soft button.

When using FLAG_ACTIVITY_REORDER_TO_FRONT I can keep the user data alive when the soft buttons are used, because each activity is reused.

But, when the user presses the Back hard button from B, B dies, due to that hard button uses finish() implicitly.

Then, I tried overriding onBackPressed in B, adding to it the same behavior as my Back soft button, thinking that the Back hard button will behave exactly like the former button (not finish B).

Now, getting back from B to A with Back hard key, everything is fine. At this point with the focus in A, when the user presses the back hard button again, the expected behavior is that the application leaves.

The problem is that expected behavior does not occur, given that B is still alive; so that overriden onBackPressed in B is still listening, and some other behavior ocurr instead.

How can I finish listening with the overriden onBackPressed in B, so that when the focus is in A the application leaves?

Thanks in advance.

Jorge Gil
  • 4,265
  • 5
  • 38
  • 57
  • Don't rely on your Activity to stay in memory when it's not visible. It's only working because the Operating System hasn't reclaimed it during your testing; there's no guarantee that it has to be there when you come back. Bottom line: save your data. – Krylez Jul 27 '12 at 23:43
  • FLAG_ACTIVITY_REORDER_TO_FRONT guarantee my Activity to be always there. – Jorge Gil Jul 28 '12 at 01:11
  • No, that flag just brings it to the front if it's still there. You cant retroactively ensure that the OS didn't kill it to regain resources. – Krylez Jul 29 '12 at 09:24

2 Answers2

0

Consider doing as Krylez's comment. Or you might want to use fragments. If you target SDKs which are older than 3.x, see support library.

There are sample projects inside SDK folder, which use wizard style.

Anh3Saigon
  • 199
  • 5
  • OK, I will try as Krylez says. @Anh3Saigon, could yo please shared one of that example's name to me? Thank you. – Jorge Gil Jul 30 '12 at 19:01
  • Sorry if I remember wrong. For instance testing, you can run an emulator, open API Demos app, browse to _App > Fragment > Stack_. For example, the source is located at `[SDK]/samples/android-13/ApiDemos/src/com/example/android/apis/app/FragmentStack.java`. – Anh3Saigon Jul 31 '12 at 07:05
  • Thank you very much Anh3Saigon. – Jorge Gil Jul 31 '12 at 17:11
0

Well, I could solved my problem with a natural Android solution:

Following the Krylez tip, I've stopped using FLAG_ACTIVITY_REORDER_TO_FRONT, so I don't have conflicts with the hard button anymore, and now I'm recycling the Intent which starts my wizard.

In A, I have a very common method which is called when the user presses the continue soft button to go to B. Activity A is just informative, so it doesn't need to put Intent's extras with user's data when going to B, like this:

    /** Called when the user presses the Continue button*/
public void continueButtonOnClick(View view) {

        Intent intent = this.getIntent();
        intent.setClass(this, StepOneRegisterWizardActivity.class);
        startActivity(intent);
}

When activity B starts, it always must seek if there are user's data available in Intent's extras, like this:

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

    getIntentExtras();
}

private void getIntentExtras() {
    Intent intent = this.getIntent();

    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        usernameEditText.setText(bundle.getCharSequence("usernameEditText"));
        passwordEditText.setText(bundle.getCharSequence("passwordEditText"));
    }
}

Now, maybe from B, the user presses any back button available (soft or hard) to back to A. In this case, we need to put the user's data in Intent's extras, like this:

    /** Called when the user presses the Back soft button*/
public void backButtonOnClick(View view) {
    onBackPressed();
}

@Override
/** Called when the user presses the Back hard button*/
public void onBackPressed() {
    finish();

    Intent intent = this.getIntent();
    intent.setClass(this, StepZeroRegisterWizardActivity.class);
    intent.putExtra("usernameEditText", usernameEditText.getText());
    intent.putExtra("passwordEditText", passwordEditText.getText());
    startActivity(intent);
}

Finally, when the user presses the continue soft button again, the new Activity B will have the data that user entered las time.

I hope it helps someone.

Jorge Gil
  • 4,265
  • 5
  • 38
  • 57