保存对象 Android 不工作

发布于 2024-10-05 02:46:26 字数 6468 浏览 0 评论 0原文

onRestoreInstanceState(捆绑已保存实例状态)

没有被调用,我在下面粘贴我的代码。实际上,我想重新绘制当我的活动进入后台时保存的所有点。

    package com.geniteam.mytest;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class Tutorial2D extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new Panel(this));
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {     
     super.onSaveInstanceState(outState);
     Panel panel = new Panel(this);
     outState.putSerializable("test", panel._graphics);     

    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {     
     super.onRestoreInstanceState(savedInstanceState);
     Panel panel = new Panel(this);
     panel.set_graphics((ArrayList<GraphicObject>) savedInstanceState.getSerializable("test"));     
    }

    class Panel extends SurfaceView implements SurfaceHolder.Callback {
        private TutorialThread _thread;        
        private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();

        public ArrayList<GraphicObject> get_graphics() {
   return _graphics;   
  }

  public void set_graphics(ArrayList<GraphicObject> graphics) {
   _graphics = graphics;
  }

  public Panel(Context context) {
            super(context);
            getHolder().addCallback(this);
            _thread = new TutorialThread(getHolder(), this);
            setFocusable(true);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            synchronized (_thread.getSurfaceHolder()) {
               // if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    GraphicObject graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
                    graphic.getCoordinates().setX((int) event.getX() - graphic.getGraphic().getWidth() / 2);
                    graphic.getCoordinates().setY((int) event.getY() - graphic.getGraphic().getHeight() / 2);
                    _graphics.add(graphic);
                //}
                return true;
            }
        }

        @Override
        public void onDraw(Canvas canvas) {
            canvas.drawColor(Color.BLACK);
            Bitmap bitmap;
            GraphicObject.Coordinates coords;
            for (GraphicObject graphic : _graphics) {
                bitmap = graphic.getGraphic();
                coords = graphic.getCoordinates();
                canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            // TODO Auto-generated method stub
        }


        public void surfaceCreated(SurfaceHolder holder) {
            try{
         _thread.setRunning(true);
            _thread.start();
            }catch(Exception ex){
             _thread = new TutorialThread(getHolder(), this);
             _thread.start();
            }

        }


        public void surfaceDestroyed(SurfaceHolder holder) {
            // simply copied from sample application LunarLander:
            // we have to tell thread to shut down & wait for it to finish, or else
            // it might touch the Surface after we return and explode
            boolean retry = true;
            _thread.setRunning(false);
            while (retry) {
                try {
                    _thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
        }
    }

    class TutorialThread extends Thread {
        private SurfaceHolder _surfaceHolder;
        private Panel _panel;
        private boolean _run = false;

        public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
            _surfaceHolder = surfaceHolder;
            _panel = panel;
        }

        public void setRunning(boolean run) {
            _run = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return _surfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (_run) {
                c = null;
                try {
                    c = _surfaceHolder.lockCanvas(null);
                    synchronized (_surfaceHolder) {
                        _panel.onDraw(c);
                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }

    class GraphicObject {
        /**
         * Contains the coordinates of the graphic.
         */
        public class Coordinates {
            private int _x = 100;
            private int _y = 0;

            public int getX() {
                return _x + _bitmap.getWidth() / 2;
            }

            public void setX(int value) {
                _x = value - _bitmap.getWidth() / 2;
            }

            public int getY() {
                return _y + _bitmap.getHeight() / 2;
            }

            public void setY(int value) {
                _y = value - _bitmap.getHeight() / 2;
            }

            public String toString() {
                return "Coordinates: (" + _x + "/" + _y + ")";
            }
        }

        private Bitmap _bitmap;
        private Coordinates _coordinates;

        public GraphicObject(Bitmap bitmap) {
            _bitmap = bitmap;
            _coordinates = new Coordinates();
        }

        public Bitmap getGraphic() {
            return _bitmap;
        }

        public Coordinates getCoordinates() {
            return _coordinates;
        }
    }
}

onRestoreInstanceState(Bundle savedInstanceState)

is not geting called, I am pasting my code below. Actually I want to redraw all the points that are saved when my activity went in background.

    package com.geniteam.mytest;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class Tutorial2D extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new Panel(this));
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {     
     super.onSaveInstanceState(outState);
     Panel panel = new Panel(this);
     outState.putSerializable("test", panel._graphics);     

    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {     
     super.onRestoreInstanceState(savedInstanceState);
     Panel panel = new Panel(this);
     panel.set_graphics((ArrayList<GraphicObject>) savedInstanceState.getSerializable("test"));     
    }

    class Panel extends SurfaceView implements SurfaceHolder.Callback {
        private TutorialThread _thread;        
        private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();

        public ArrayList<GraphicObject> get_graphics() {
   return _graphics;   
  }

  public void set_graphics(ArrayList<GraphicObject> graphics) {
   _graphics = graphics;
  }

  public Panel(Context context) {
            super(context);
            getHolder().addCallback(this);
            _thread = new TutorialThread(getHolder(), this);
            setFocusable(true);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            synchronized (_thread.getSurfaceHolder()) {
               // if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    GraphicObject graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
                    graphic.getCoordinates().setX((int) event.getX() - graphic.getGraphic().getWidth() / 2);
                    graphic.getCoordinates().setY((int) event.getY() - graphic.getGraphic().getHeight() / 2);
                    _graphics.add(graphic);
                //}
                return true;
            }
        }

        @Override
        public void onDraw(Canvas canvas) {
            canvas.drawColor(Color.BLACK);
            Bitmap bitmap;
            GraphicObject.Coordinates coords;
            for (GraphicObject graphic : _graphics) {
                bitmap = graphic.getGraphic();
                coords = graphic.getCoordinates();
                canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            // TODO Auto-generated method stub
        }


        public void surfaceCreated(SurfaceHolder holder) {
            try{
         _thread.setRunning(true);
            _thread.start();
            }catch(Exception ex){
             _thread = new TutorialThread(getHolder(), this);
             _thread.start();
            }

        }


        public void surfaceDestroyed(SurfaceHolder holder) {
            // simply copied from sample application LunarLander:
            // we have to tell thread to shut down & wait for it to finish, or else
            // it might touch the Surface after we return and explode
            boolean retry = true;
            _thread.setRunning(false);
            while (retry) {
                try {
                    _thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
        }
    }

    class TutorialThread extends Thread {
        private SurfaceHolder _surfaceHolder;
        private Panel _panel;
        private boolean _run = false;

        public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
            _surfaceHolder = surfaceHolder;
            _panel = panel;
        }

        public void setRunning(boolean run) {
            _run = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return _surfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (_run) {
                c = null;
                try {
                    c = _surfaceHolder.lockCanvas(null);
                    synchronized (_surfaceHolder) {
                        _panel.onDraw(c);
                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }

    class GraphicObject {
        /**
         * Contains the coordinates of the graphic.
         */
        public class Coordinates {
            private int _x = 100;
            private int _y = 0;

            public int getX() {
                return _x + _bitmap.getWidth() / 2;
            }

            public void setX(int value) {
                _x = value - _bitmap.getWidth() / 2;
            }

            public int getY() {
                return _y + _bitmap.getHeight() / 2;
            }

            public void setY(int value) {
                _y = value - _bitmap.getHeight() / 2;
            }

            public String toString() {
                return "Coordinates: (" + _x + "/" + _y + ")";
            }
        }

        private Bitmap _bitmap;
        private Coordinates _coordinates;

        public GraphicObject(Bitmap bitmap) {
            _bitmap = bitmap;
            _coordinates = new Coordinates();
        }

        public Bitmap getGraphic() {
            return _bitmap;
        }

        public Coordinates getCoordinates() {
            return _coordinates;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2024-10-12 02:46:26

onRestoreInstanceState 仅在应用程序被销毁并重新创建时才会被调用。将应用程序置于后台然后再次启动将调用 onResume,但不会调用 onRestoreInstanceState。

onRestoreInstanceState only gets called if the app is destroyed and re-created. Putting the app in the background then bringing it up again will call onResume, but not onRestoreInstanceState.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文