0

I have 2 Activities: DetailsBuildingActivity and EditBuildingActivity. DetailsActivity has a button to go to the EditBuildingActivity, to edit properties of the building (like name, size, etc). After editing and saving, you the DetailsBuildingActivity opens up with the new adjusted data. Doing this a few times back and forth in 1 go, gives me the following problem:

When doing 5 times in a row, but then I press the backbutton, I have to press it 10 times before I can get to my previous Activity. (10 times - 5 times DetailsBuildingActivity and 5 times EditBuildingActivity). Is there a way to solve this? I constantly make an intent to open each other op.

mXX
  • 3,595
  • 12
  • 44
  • 61

1 Answers1

0

You need to use the activity stack.

When you save in the EditBuildingActivity you need to call finish() and not create a new intent.

If you would like DetailsBuildingActivity to know about EditBuildingActivities status, you can use

startActivityForResult(intent, requestCode) to start the activity.

setResult() && finish() to complete the launched activity

and

--- ANDROID DOCS

protected void onActivityResult (int requestCode, int resultCode, Intent data)

Since: API Level 1 Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation. You will receive this call immediately before onResume() when your activity is re-starting. Remember that activities can have multiple instances, and every time you start a new one with a intent they are typically added to the top of your activity stack.

HaMMeReD
  • 2,440
  • 22
  • 29
  • You say "When you save in the EditBuildingActivity you need to call finish() and not create a new intent." But I'm passing data with `putExtra`, is there then a way around it? – mXX Jun 19 '13 at 11:13
  • And ow yeah like that, when editing and then starting a new activity, the new data gets loaded in the detailActivity – mXX Jun 19 '13 at 13:01
  • onActivityResult handles all of these things, from the docs. You load the data in the parent activity onResume() protected void onActivityResult (int requestCode, int resultCode, Intent data) Since: API Level 1 Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation. You will receive this call immediately before onResume() when your activity is re-starting. – HaMMeReD Jun 19 '13 at 18:16
  • I think I understand a bit, can you show me an example pleasE? – mXX Jun 19 '13 at 18:19
  • http://www.mybringback.com/tutorial-series/12204/onactivityresult-android-tutorial/ http://saigeethamn.blogspot.ca/2009/08/android-developer-tutorial-for_31.html – HaMMeReD Jun 19 '13 at 19:04