How do I save the state of my activity?

This question already has an answer here:

  • Saving Android Activity state using Save Instance State 25 answers

  • You're going to have to implement onSavedInstanceState() and populate it with the int for the view you're displaying.

    Then, in your onCreate(Bundle savedInstanceState) method, you're going to dig the int out of the bundle and set your content view to that.

    public class yourActivity extends Activity {
    
        private static final String KEY_STATE_VIEW_ID = "view_id";
        private int _viewId = R.layout.levels;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            if (savedInstanceState != null) {
                if (savedInstanceState.containsKey(KEY_STATE_VIEW_ID) {
                    _viewId = savedInstanceState.getInt(KEY_STATE_VIEW_ID);
                }
            }
            setContentView(_viewId);
            // in your onClick set viewId to R.layout.social
        }
    
        @Override
        public void onSaveInstanceState(Bundle savedInstanceState){
            super.onSaveInstanceState(savedInstanceState);
            savedInstanceState.putInt(KEY_STATE_VIEW_ID, _viewId);
        }
    }
    

    An example can be found here: http://www.how-to-develop-android-apps.com/tag/onsaveinstancestate/

    链接地址: http://www.djcxy.com/p/26192.html

    上一篇: 保存应用程序的状态

    下一篇: 我如何保存我的活动状态?