Android app discards orientation changes, the best way to handle it?

So, I am making a basic chess application to play with some elements of android programming, and still I am learning a lot, but this time I got lost.

When the emulator orientation changes, the activity gets reset. Based on my research, the same thing will happen at any time when the application is paused / interrupted, i.e. keyboard change, phone call, pressing the home key, etc.

Obviously, there is no constant game of chess reset, so again I want to find out how to fix this problem.

In my research, you will learn a few basic things by overriding the onPaused method in my activity, listening to Orientation, changing the keyboard in my manifest (via android: configChanges), using Parcelables or Serialization.

I searched for a lot of code examples using Pacelables, but to be honest, this is too confusing. Maybe returning tomorrow with new eyes will be useful, but now, the more I look at Parcelables, the less it feels.

My application uses a Board object that contains 64 Cell objects (in an 8x8 2D array) and each cell has a Piece object, either the actual fragment, or zero if the space is empty. Assuming I am using Parcelable or Serialization, I assume that I would have to Parcelize or Serialize each class, Board, Cell and Piece.

First of all, is Parcelable or Serialization even the right thing to look at this problem? If so, is Parcelable or Serializable preferable for this? And do I correctly assume that each of the three objects should be delayed / serialized? Finally, does anyone have a link to an easy-to-understand Parcelable tutorial? All that will help me understand and stop further headaches in the future when my application expands even further.

Any help would be appreciated.

+4
source share
5 answers

in your manifest in the <Activity> tag, you can add android: configChanges = "orientation | keyboardHidden", this will stop the reload action and call onConfigurationChanged () instead of changing the orientation or hiding the keyboard.

If you need to make adjustments when one of these events happens, you can override onConfigurationChanged () in your activity, if not all you need to do is add the property to the manifest.

Sort of:

@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(R.layout.myLayout); } 

works great.

+8
source

The default Androids method for handling events is to recreate activity. Basically you process one process correctly and everything works, no need to worry about how to handle these things manually.

Application Basics provides a complete overview of the activity life cycle, but in short you want to save your activity state in onSaveInstanceState () and use the Bundle that you get in onCreate (Bundle savedInstanceState) to restore the state of your application.

If you want to keep your classes in the Bundle, it is best to implement the Parcelable interface. Then, to save your state, follow these steps:

 protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable("yourObject", mYourObject); } 

and in the onCreate method, which you just execute:

 if (savedInstanceState != null) mYourObject = savedInstanceState.getParcelable("yourObject"); 

Of course, you can simply convert your objects to regular array views that the Bundle can already contain, and just skip the implementation of the Parcelable interface. Basically add the toArray () method to your object and the static fromArray () method. Well, play around and see what you feel better.

+3
source

Or stick to this line in your OnCreate so that it doesn't hang. The problem is resolved.

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
+3
source

Override onRetainNonConfigurationInstance in the Activity class.

In this method, you have to return the object, just bind your game state in one state object and return it in this method. Make sure that this is only a state object, so I mean that it should not contain any handles for the activity, view, etc. contained in it, or you will lose a memory leak.

In the onCreate method, call getLastNonConfigurationInstance to return the object.

You do not need to worry about implementation details (serialization) android will take care of this.

If you have not yet made sure that you set your launch mode to manifest in either singleTask or singleInstance, depending on what suits your needs best. By default, if someone gets home and then returns to your application, he starts a new copy of the action, if it is not processed or configured for one instance, you will receive several copies of your game activity.

+1
source

When you save the state of the board, make int[64] and save the corresponding fragment in each position. So, 0 = empty, 1 = white pawn, 2 = white knight, etc.

When you load the state of the board, iterate over the array and create the corresponding piece objects in the appropriate places.

You can convert int [64] to a string for storage in SharedPreferences, or use it with Parcelable or whatever. Save only minimal data.

+1
source

Source: https://habr.com/ru/post/902660/


All Articles