Android app resets on orientation change, best way to handle?

后端 未结 5 553
余生分开走
余生分开走 2021-01-03 08:02

So I am making a basic chess app to play around with some various elements of android programming and so far I am learning a lot, but this time I am lost.

When the o

相关标签:
5条回答
  • 2021-01-03 08:10

    Androids default way of handling the events is to recreate the activity. Basically you handle one process correctly and everything works, no need to worry about handling those things manually.

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

    If you want to store your classes in the Bundle your best bet is to implement the Parcelable interface. Then to save your state you do:

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

    and in the onCreate method you simply do:

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

    Of course you could just convert your objects into normal array representations that the Bundle can already contain and just skip implementing the Parcelable interface. Basically add a method toArray() to your object and a static method fromArray(). Well, play around and see which suits you better.

    0 讨论(0)
  • 2021-01-03 08:14

    Override onRetainNonConfigurationInstance in your Activity class.

    In this method you must return an object, just bundle up your game's state in a single state object and return it in this method. Make sure this is only a state object, by that i mean it should have no handles to an activity, view, etc. contained within it or you'll get memory leaking.

    In your onCreate method call getLastNonConfigurationInstance to get the object back.

    You don't have to worry about the implementation details (the serialization) android takes care of that.

    If you haven't already make sure you've set your Launch Mode in the manifest to either singleTask or singleInstance depending on which fits your needs better. By default if someone hits home and then comes back to your application it launches a new copy of the activity, if not handled or configured for single instance you'll get multiple copies of your game activity running.

    0 讨论(0)
  • 2021-01-03 08:15

    Or stick this line in your OnCreate so it doesn't roate. Problem solved.

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    0 讨论(0)
  • 2021-01-03 08:20

    When you save the board state, make an int[64] and at each position store the corresponding piece. So 0=empty, 1=white pawn, 2=white knight, etc...

    When you load the board state, iterate through the array and create the appropriate piece objects at the appropriate locations.

    You can convert the int[64] to a string to store in SharedPreferences, or use it with a Parcelable or whatever. Only store the minimum data you need.

    0 讨论(0)
  • 2021-01-03 08:25

    in your manifest in the <Activity> tag, you can add android:configChanges="orientation|keyboardHidden", this will stop the activity from reloading and call onConfigurationChanged() instead when the orientation is changed or the keyboard is hidden.

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

    Something like:

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

    works perfectly well.

    0 讨论(0)
提交回复
热议问题