0

My question is how to update an activity based on its child while maintaining the context supplied by its parent.

See the example hierarchy below. There is a flow up and down. Using the app would go something like this:

Select Team 1 on the Home activity --> Activity A inflates based on the selection.
Select Player 2 on Activity A --> Activity B inflates based on the selection.
Select to Add a Stat on Activity B --> Activity C inflates and allows a stat to be added.

User returns to Activity B, which must maintain the Player 2 context and update based on C.
User returns to Activity A, which must maintain the Team 1 context and updated based on B.
User returns to Home, which must update based on A.

Both Activities A and B inflate based on parent selections, but must update if a change is made in a child activity. enter image description here

For example, let's focus on Activity B. It knows to inflate for Player 1 because this info was passed from Activity A in an intent.

  1. If I were to go to Activity C and press Back, B wouldn't update.
  2. If I were to go to C and press Up, B would crash because it would try to make a new instance and wouldn't know what player to inflate (defined in onCreate()).

I tried declaring B with a "singleTop" launchMode, but then it just restarts and doesn't update based on C's info. Based on my limited experience/research, I see the following options.

  1. Store an Activity's inflater variables as static (so they won't be "lost" when creating a new instance)
  2. Initiate my view inflation (via Fragment) in the Activity's onStart() method so that it would refresh when Restarting. (Would this help? What about the fact that all of my inflation happens inside of a fragment? I fear the Lifecycle gods.)

I'm new to a lot of this and would appreciate any help. Thanks!

EDIT: I've tried making "player" a static variable in Activity B and letting Activity A set it. This seems to be working. I can remove the "singleTop" launch mode and then navigating up from C loads a new instance of B with the info refreshed.

However, this feels like a workaround and I would like someone to comment on this approach and maybe offer an alternative. Thanks.

NSouth
  • 5,067
  • 7
  • 48
  • 83

1 Answers1

0

Have you considered using fragments and your home activity handling all the transaction and backstack with arguments and calls to fragment internal methods to update UI?

If you really like to use activities, then you should use start activity for result. You can pass result to calling-activity and update accordingly.

Getting a Result from an Activity

uDevel
  • 1,891
  • 1
  • 13
  • 14
  • `startActivityForResult` seems like a good way to go. I plan to use it. In this case, I would only have to use `onActivityResult()` instead of refreshing part of my UI on `onStart()` as you suggested in my [other question](http://stackoverflow.com/questions/23372201/can-i-create-my-ui-fragment-in-my-activitys-onstart/23372306#23372306), right? Regarding using fragments, I've already coded these different activities, but I'd like to know what you mean and what advantages that approach could provide. Could you give a summary or link to a guide? Thanks for the answers. – NSouth Apr 29 '14 at 19:05
  • 1
    From my experience, there are tons of benefits to use fragments. http://developer.android.com/guide/components/fragments.html For one thing, fragment is the way to go for Android in the future, each update of the API adds more functions to fragment, while activites not so much. So you have to use it soon or later anyways. But you learned how to write Android apps with activities, then it might take a few hello world apps to learn fragment. I learned Android right when fragment is starting to get popular, so I jumped right into fragment. – uDevel Apr 29 '14 at 19:18
  • Thanks, @uDevel. I'll look into using activities more. Really, I only use activities to load fragments so far, but so far one activity has always had one fragment. I'll look into using an activity to manage multiple fragments. – NSouth Apr 29 '14 at 19:39