Android app crashes if the orientation changes

2019-03-03 09:47发布

问题:

I have created a game app where I share some details to Facebook through a button called Share. When I finish the game it asks for the name in a dialog box, it shows no problem when working on the same orientation (landscape/portrait). If I'm playing in landscape, finish the game, flip the phone to portrait to type my name in, then click "Share" it will crash. How to overcome this problem?

回答1:

An activity is restarted when the orientation changes. I'm guessing your code isn't saving needed information before this restart occurs. You can stop this default behavior by handling specific configuration changes yourself (ie: orientation change). A good tutorial on doing this is located here: Handling Runtime Changes