0

In Android, startActivityForResult() and onActivityResult() can be used to pass a bundle from an activity to its PARENT_ACTIVITY (ref this question, for instance, or this question). This requires overriding onBackPressed() to call finish() instead.

The semantics of this feel "wrong" to me in a case where pressing back isn't about finishing some step in a flow and returning a value. In my case, when the user presses back, they're effectively saying "I changed my mind and want to go back to the previous screen" not "I'm done with the current screen and want to go forward," which is how finish() feels to me.

Is there another alternative where I just want the activity I'm going back to to know some info about state of the current activity? Or is the consensus in the community that startActivityForResult is the right way to go, regardless of the semantics/intent?

Details follow:

I am working on an app including two activities centered around Google Maps for Android, SelectActivity and EditActivity. SelectActivity shows multiple points of interst on the map and allows the user to select an existing POI or create a new POI. EditActivity allows the user to edit details.

EditActivity is defined in the manifest with SelectActivity as its PARENT_ACTIVITY, so pressing the back button in EditActivity returns to SelectActivity. There are some other activities that allow the user to drill down deeper, after EditActivity, but they are not relevant to this question.

When SelectActivity launches EditActivity, it includes a bundle with the current map parameters in the Intent, so EditActivity starts with the same map view as SelectActivity.

Right now, SelectActivity sets the map to an area around the device's current location from Location Services. This is the desired behavior in most cases because the user will usually want to be editing things around their immediate location.

However, sometimes users need to edit multiple things somewhere else on earth. Ideally, when SelectActivity is resumed by returning from EditActivity, a bundle containing the current map parameters (center, zoom level, etc) would be created and passed, and then SelectActivity would use the map parameters from that bundle instead of the default device-centered behavior.

Community
  • 1
  • 1
Alterscape
  • 1,526
  • 1
  • 17
  • 34
  • 1
    I think you're a little confused. The default behavior of the back button is to call finish(). Overriding onBackPressed is needed to set a result, if you haven't already. Normally you have already. You can also set a result and call finish on any other event, such as selecting an item or pressing a button. – Gabe Sechan Jan 05 '17 at 21:12
  • 1
    Also, finish does not mean "take me to the next step". It means "end this activity". Which, if this activity is still the foreground activity, will take you to the previous activity. – Gabe Sechan Jan 05 '17 at 21:15
  • 1
    ANother mistake- the parent activity does not, I believe, change the behavior of the back button. It changes the behavior of the UP button on the taskbar. The back button continues to take you backwards in the stack. – Gabe Sechan Jan 05 '17 at 21:18
  • 1
    However you can get the behavior you want. When the user finishes editing (whether that's by pressing back or by doing something else) pop the zoom level, location, and all the other data into the result intent. In SelectActivity.onActivityResult, read those parameters and adjust your map accordingly. – Gabe Sechan Jan 05 '17 at 21:22
  • Thank you for the clarifications, Gabe! – Alterscape Jan 05 '17 at 21:42

0 Answers0