Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top