jomni
jomni

Reputation: 789

onActivityResult only called once

I have the following Activity sequence in a folder picker part of an app:

Activity A starts B. B then starts instances of itself that stack as the user goes deeper into the folder tree. Whenever an instance of B is completed (folder chosen), it and all the preceding Bs should finish and A should be shown again, with the propagated result. If the user backtracks, the preceding Bs should not finish, since the user should be able to go back and forth between folder subtrees.

I'm trying to accomplish this by using startActivityForResult, both from A to B and then for each new instance of B. When the user finishes an instance B, I use setResult and want to check it in each preceding instance's onActivityResult method. If it's RESULT_OK, I set the result again and finish that instance, until all of them are finished in a cascading sequence and A is shown again with the result.

The sequence works as long as I go A => B(1) => B(2) => B(3) and finish, without backtracking in the B part.

However, if I go A => B(1) => B(2) => back to B(1) => B(3) and finish, onActivityResult is only called in B(1) when going back from B(2) the first time, not when I later go back to it when finishing from B(3). B(1) is simply resumed when going back from B(3) in this sequence. This breaks the cascading finish sequence.

Why is onActivityResult only called once for instance B(1)? Shouldn't it be called each time a child activity sets a result and finishes?

Here's some code:

=== Activity A:

Starting the first B instance:

Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(intent, RequestCodes.ActivityB);

Receiving the final B result:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(resultCode == RESULT_OK){
    ...
  }
}

=== Activity B:

Starting new child B from itself:

Intent intent = new Intent(ActivityB.this, ActivityB.class);
startActivityForResult(intent, RequestCodes.ActivityB);

Setting the result when choosing a folder:

Intent result = new Intent();
result.putExtra(...);
setResult(Activity.RESULT_OK, result);
finish();

Receiving the child B result in each parent B instance (which is only called the first time in B(1) for some reason):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(resultCode == RESULT_OK){
    setResult(Activity.RESULT_OK, data);
    finish();
  }
}

Thanks in advance.

Upvotes: 2

Views: 671

Answers (1)

David Wasser
David Wasser

Reputation: 95568

This is a horrible architecture! You shouldn't create multiple instances of B when traversing a tree. Eventually you will run out of memory! You should use a single instance of your Activity and when the user moves up or down in the tree you should just change the data that you display in the Activity. You should have an internal data model that describes the tree and you only need to remember what node in the tree you are currently displaying. If you also want to support the user going back (by using the BACK button), you can create your own stack (not a stack of activities, but just a stack of tree node references) that you can use to determine which node of the tree to go back to from the current node. This is a simple data management problem and your architecture has forced this to become a View management problem.

Upvotes: 1

Related Questions