0

I have a situation where I have a three step registration process. Each step is contained in its own activity - call them A, B, and C. Only when I get to the end of step three, can I reliably validate one of the inputs (and email address to which I send a confirmation using PHPMailer). If this comes back with an error, I want to go all the way back to A, which is where the email address is entered.

This is easy enough by just calling an Intent. However, I want to retain the input values that the user entered originally so that they don't have to enter them again. Theoretically, the easiest way to do this is to call finish() twice. But this doesn't work since calling it once transfers control back to B and the second call never happens.

Is there a simple way to do this, or do I have to call Intent and then somehow populate the fields through another method? Thanks!

AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • Have a look at this [answer](http://stackoverflow.com/questions/18229797/closing-application-and-killing-it-from-ram/18229915#18229915) - *hint* the clue is in the flags.... – t0mm13b Aug 14 '13 at 17:26

2 Answers2

1

I'm not sure I completely get you but you could use the flag FLAG_ACTIVITY_CLEAR_TOP to bring it back to A and remove B.

Intent i = new Intent(C.this, A.class)
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();

Run this code when you get the error and it will bring A back up. And if the "Back" button is pressed you will still get to B.

Another option you have is to store the information in something like SharedPreferences and load the data from there but this may be unnecessary for your situation.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • This seems like it should work. However, when I get back to A, all of my fields are cleared, which is exactly what I am trying to avoid. One change to your code that I had to make was to the second line, which I had to make `i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP` to avoid the error `FLAG_ACTIVITY_CLEAR_TOP cannot be resolved to a variable`. Do you agree with this change or should I handle it differently? Thanks! – AndroidDev Aug 14 '13 at 17:44
  • No, that is correct. Sorry, I don't usually forget that. I will edit my answer – codeMagic Aug 14 '13 at 17:47
  • Then store them in `SharedPreferences` as I suggested and reload the data frome there – codeMagic Aug 14 '13 at 17:48
-1

To pass data around between activities, use a Bundle, for an example, in this class activity - B

Intent intent = new Intent(this, C.class);
Bundle b = new Bundle();
b.putBoolean("some_boolean_value", true);
b.putInt("some_int_value", 4);
b.putString("some_string_value", "foobar");
intent.putExtra("fooBundle", b);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();

Then, from your class activity 'C', after it appears on screen, from within the onCreate method, extract the bundle:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState.containsKey("fooBundle")){
        Bundle b = savedInstanceState.getBundle("fooBundle");
        int nVal = b.getInt("some_int_value");
        String sVal = b.getString("some_string_value");
        boolean blnVal = b.getBoolean("some_boolean_value");
        // Do whatever you have to do, put them into textview's etc.
    }
}

Notice, how the check is made to ensure there is indeed a bundle, if not, its an "empty view" i.e. no data to prefill into the fields. And reverse the pulling out the values from the Bundle.

When you back out of the class activity C, it goes back to activity A.

Edit

The OP is insistent on using "two" back key strokes which is deemed unnecessary as the flag indicating the activity is cleared from the stack.

A -> B

Before creating the intent in the above code and runs, the stack now becomes...

A -> C

Now back out of the activity C, by pressing the back key.

A

Community
  • 1
  • 1
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • Thanks for the suggestion. I know that I can do it that way (or using shared preferences). Just seeing if there is a quick way to simulate two 'back' clicks. I may very well wind up doing it like this, though. Thanks! – AndroidDev Aug 14 '13 at 17:47
  • Shared preferences are not the way to go in doing something like this. – t0mm13b Aug 14 '13 at 17:48
  • 1
    I'm not sure how this answers the question. The question wasn't about how to pass data around but how to get back to A with an error and remove B. With this code, if the user presses back on C then they will be taken to A but that's not what they want – codeMagic Aug 14 '13 at 18:16
  • Thanks for this post. However, after implementing it, it still doesn't work. I get an empty screen A, exactly what I am trying to avoid. Also, I have to code around a null pointer error with the savedInstanceState bundle on the initial entry into A. – AndroidDev Aug 14 '13 at 18:27
  • @codeMagic The OP asked this which is what I thought was also part of the question *Is there a simple way to do this, or do I have to call Intent and then **somehow populate the fields through another method** ? Thanks!* – t0mm13b Aug 14 '13 at 18:32
  • Ahhhhh! Gotcha, I misinterpreted as passing data through A->B->C - not the other way around which is what the OP is looking for? In that case, use `this.startActivityForResult(intent, requestCode);` from activity A, when finished in activity C, pass the bundle back in with the `requestCode` as dictated by the parameter. – t0mm13b Aug 14 '13 at 18:37
  • I see. What is wrong with `SharedPreferences` in this situation? To retain the data when going back to A after an authentication error, I mean. I agree it is unnecessary just to send the data to B and C but this would save it in case of an error. And it would be there later if necessary – codeMagic Aug 14 '13 at 18:37
  • It not always going back to A...only when there is an authentication error and the user has to go back to A to input the correct information. – codeMagic Aug 14 '13 at 18:38
  • Would this [help](http://stackoverflow.com/questions/10407159/android-how-to-manage-start-activity-for-result) – t0mm13b Aug 14 '13 at 18:39
  • To clarify: The activities build chronologically from A->B->C. After validating in C, there is an error situation where I want to go all the way back to A. I'd like to have the fields in A be populated with the user's original entries so that they don't have to re-input everything. – AndroidDev Aug 14 '13 at 18:41
  • 1
    Thanks for clarifying, in that case, why not save the data in the bundle in the `onSaveInstanceState`, that is in activity A, launch the intent to activity B, then launch the activity C, if that fails, the activity C is popped off stack back to Activity A, the `onRestoreInstanceState` will be fired and the values are there? – t0mm13b Aug 14 '13 at 18:42
  • SharedPreferences are limited - rather more for application preferences, not for passing data around. You can store arrays, objects etc in a bundle and is more optimized as it gets serialized into what is essentially a `Parcelable` and faster retrieval over xml. – t0mm13b Aug 14 '13 at 18:53