我如何保存我的活动状态?

这个问题在这里已经有了答案:

  • 使用保存实例状态保存Android活动状态25个答案

  • 你将不得不实现onSavedInstanceState()并使用你正在显示的视图的int来填充它。

    然后,在您的onCreate(Bundle savedInstanceState)方法中,您将从该包中挖掘出整型,并将您的内容视图设置为该内容视图。

    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);
        }
    }
    

    一个例子可以在这里找到:http://www.how-to-develop-android-apps.com/tag/onsaveinstancestate/

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

    上一篇: How do I save the state of my activity?

    下一篇: How to save an activity's state?