I always wondered what is the benefit of destroying activity on rotation? I mean you can
specify android:configChanges="orientation|screenSize"
and it won't get destroyed
but what's the reason for destroying it

- 3,783
- 2
- 21
- 30
-
possible duplicate of [What is the advantage of letting an activity be destroyed on rotation?](http://stackoverflow.com/questions/13647903/what-is-the-advantage-of-letting-an-activity-be-destroyed-on-rotation) – Emmanuel Dec 01 '14 at 17:28
-
1@Emmanuel That thread should be required reading for every Android developer. How often do we see `android:configChanges="orientation` as a "solution"? – Simon Dec 02 '14 at 21:05
2 Answers
Because generally the UI gets all messed up when we have built the screen for portrait mode(say) and the screen gets rotated, so android os thinks that we might need to refactor our layout or maybe use a new layout during onOrientationChanged, maybe that's why the activity gets created again

- 5,794
- 2
- 24
- 44
-
And If in my case I have the same xml for both portrait and landscape, is it a wrong to force it not to destroy it? Are there any reasons except the one you mention? – user3364192 Dec 01 '14 at 17:30
-
-
@KristyWelsh `android:configChanges="orientation"` in `activity` element inside the manifest does the trick. – aga Dec 01 '14 at 17:33
-
@user3364192 i just shared my point of view, maybe there is a more deeper reason or maybe it might just be that google had 2 options (to destroy or not to) and they went with the former :) – Sarthak Mittal Dec 01 '14 at 17:33
-
@aga - true if you never change the orientation, but if orientation is changed, activity is recreated. – Kristy Welsh Dec 01 '14 at 17:43
-
@KristyWelsh if you've stated that your activity shouldn't be recreated after rotation via `android:configChanges="orientation"` then Android won't recreate it. – aga Dec 01 '14 at 17:50
-
3@aga `android:configChanges="orientation"` this is a hack, and a dangerous one. I reckon it's the cause of half the bugs in Android apps. Why, because you don't then bother to code the life cycle properly and your activity is destroyed because of one of the other reasons such as keyboard changed, incoming call, locale changed etc. Google are quite clear on this. Only use it as a last resort and only if you really know what you are doing. Do not use this hack. – Simon Dec 01 '14 at 18:53
-
@Simon thanks for sharing the knowledge :) but is there any other way to handle this then? – Sarthak Mittal Dec 01 '14 at 18:55
-
@Simon where did I say that it's recommended in any way? all I said is that you can prevent Activity from being recreated, nothing more. if you want to drill a hole in the wall, I'll give you a drill, but I expect that you're able to read the instruction before using it. – aga Dec 02 '14 at 09:32
-
@aga I didn't say you said it was recommended but you did say it would do the trick, which it won't (since there will be remaining bugs). – Simon Dec 02 '14 at 12:07
-
@SarthakMittal The only correct solution (for 99% of scenarios) is to correctly code onCreate, onPause, onStop and onResume and if need, the overrides for savng and restoring the instance state. As a community, this should be our advice everytime. If you do this, you do not need to ever concern yourself about whether an activity is destroyed or not as your app will just work. – Simon Dec 02 '14 at 12:09
-
sorry or miss-understanding but why is the orientation comes to onStop only not onStop then onDestroy – Dasser Basyouni Dec 24 '16 at 02:17
Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout)
also mathematically aspect ratios change - all these constitute to the os recreating the activity to know how to do work -(this might not be 100% true- but true)..

- 10,730
- 4
- 31
- 59