安卓:Parcelable.writeToParcel和Parcelable.Creator.createFromParcel从来不被称为 [英] Android: Parcelable.writeToParcel and Parcelable.Creator.createFromParcel are never called

查看:1424
本文介绍了安卓:Parcelable.writeToParcel和Parcelable.Creator.createFromParcel从来不被称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是全新的,以张贴在这里的问题,但我一直在阅读了很多关于这里好几年了。通常,我总是能够通过彻底的在网上搜索找到我的答案,但这个时候我很茫然......

I'm totally new to posting questions on here, however I have been reading a lot on here for years. Normally I always am able to find my answers by thoroughly searching the web, but this time I am at a loss...

花了试图弄清楚这是为什么不工作,我决定寻求帮助又一天,希望你们能够给我一些指点,或更好的,一个解决方案之后。

After having spent yet another day of trying to figure out why this is not working I decided to ask for help, hoping you guys can give me a few pointers, or better, a solution.

问题: 在Android游戏我来的地步,我必须使应用程序记住它的状态,当用户如presses的主屏按钮。经过一番搜索,我意识到,为了使我的课初始化回到他们适当的状态后,重新打开应用程序,我不得不支持Parcelable接口与包传递它们。

The problem: In an Android game I have come to the point where I have to make the application remember its state when a user e.g. presses the HOME-screen button. After some searches I realised that in order to make my classes initialize back to their appropriate states after re-opening the application I had to support the Parcelable interface to pass them with the Bundle.

在我的onStop和ONSTART功能我分别保存和恢复比赛状态,并从一个包,但是当我打电话putParcelable和getParcelable功能上的捆绑对象的writeToParcel和createFromParcel功能不会被调用。

In my onStop and onStart functions I respectively save and restore the game state to and from a Bundle, however when I call the putParcelable and getParcelable functions on the Bundle the object's writeToParcel and createFromParcel functions are never called.

由于担心这可能是由于游戏相对复杂我算了一下,最好创建一个非常简单的应用程序,试图得到它的工作。

Fearing that this may have been due to the relative complexity of the game I figured I had best create a very simple application to try to get it to work.

根据我看到网上很多Parcelable的例子,这成了我的类:

Based on many Parcelable examples I have seen online, this became my class:

public class ParcelableTest implements Parcelable {
  int id;

  public ParcelableTest(int newID)
  {
    id = newID;
  }

  private ParcelableTest(Parcel in) {
      readFromParcel(in);
  }

  public void writeToParcel(Parcel out, int arg1) {
      writeToParcel(out);
  }

  public void writeToParcel(Parcel out) {
    Log.v("ParcelableTest","Writing to parcel");
      out.writeInt(id);
  }

  public void readFromParcel(Parcel in) {
      id = in.readInt();
  }

  public int describeContents() {
      return 0;
  }

  public static final Parcelable.Creator<ParcelableTest> CREATOR = new
  Parcelable.Creator<ParcelableTest>() {
      public ParcelableTest createFromParcel(Parcel in) {
          Log.v("ParcelableTest","Creating from parcel");
              return new ParcelableTest(in);
      }

      public ParcelableTest[] newArray(int size) {
              return new ParcelableTest[size];
      }
  };
}

和从我的主要活动我会调用以下函数保存/恢复数据:

And from my Main activity I would call the following functions to save / restore the data:

public Bundle saveToBundle(Bundle savedState)
    {
      savedState.putParcelable("Test1",mTest1);
      savedState.putParcelable("Test2",mTest2);
      return savedState;
    }
    public void restoreFromBundle(Bundle savedState)
    {
      mTest1 = savedState.getParcelable("Test1");
      mTest2 = savedState.getParcelable("Test2");

    }

但由于某些原因没有的功能(与putParcelable和getParcelable功能)将导致在我的测试类的适当Parcelable呼叫。

But for some reason neither of the functions (with the putParcelable and getParcelable functions) will result in the appropriate Parcelable calls in my test class.

最奇怪的是,它以某种方式读取正确的值(我在班上试图与更多的变量),但我的调试,我的日志表明,塔应用程序不会继续writeToParcel和createFromParcel。

The strangest thing is that it does somehow read the correct values (I have tried with more variables in the class), but my debugging and my log shows that tha application never gets to writeToParcel and createFromParcel.

我是什么在这里失踪?

任何帮助/想法将AP preciated。

Any help / thoughts would be appreciated.

推荐答案

显然,Android的捆绑类不坚持的 parcelable协议的,与其随后在IPC编组。

Apparently the Android Bundle class does not adhere to the parcelable protocol that instead is followed during IPC marshalling.

相反,它似乎像束实现只写和读Parcelable对象到其自己的内部地图,通过反射的方式。从测试中我们所做的,似乎包写入/读取您的Parcelable派生类中定义的每一个领域,因为你已经宣布这些领域。

Instead, it seems like the Bundle implementation just writes and reads the Parcelable object into its own internal map by means of reflection. From a test we did, it seems that the Bundle writes/reads every field defined in your Parcelable-derived class, just because you have declared those fields.

这篇关于安卓:Parcelable.writeToParcel和Parcelable.Creator.createFromParcel从来不被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆