首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >getLastNonConfigurationInstance()的问题-似乎只返回null

getLastNonConfigurationInstance()的问题-似乎只返回null
EN

Stack Overflow用户
提问于 2010-09-29 05:23:42
回答 2查看 1.4K关注 0票数 1
代码语言:javascript
复制
public class XPBN extends Activity{
private Map _map;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    final Map data = (Map)getLastNonConfigurationInstance();
    if (data == null) {
        Toast.makeText(this, "New Map", Toast.LENGTH_SHORT).show();
        _map = new Map(this);
    } else {
        Toast.makeText(this, "Old Map", Toast.LENGTH_SHORT).show();
        _map = (Map)data;
    }

    setContentView(_map);
}

@Override
public Object onRetainNonConfigurationInstance() {
    Toast.makeText(this, "Saving Configuration...", Toast.LENGTH_LONG).show();
    return _map;
}
}

我将假设我给这个论坛的标题-线程相当彻底地陈述了我正在经历的问题。我还编辑了代码,尝试保存一个string对象,然后通过getLastNonConfigurationInstance()恢复string对象,看看我能在多大程度上让它为我工作,但它似乎仍然返回null。我没有尝试过从onStart()、onRestart()或onResume()调用它,但根据我所读到的内容,它通常只从onCreate(Bundle)调用。这让我很困惑..。:/

我认为稍微了解一下我的Map类对象可能会有所帮助,所以下面是其中的(一些)代码:

代码语言:javascript
复制
public class Map extends SurfaceView implements SurfaceHolder.Callback{
private MapThread _mapThread;
private int _terrain[][];
private ArrayList<Player> playerList;
private Bitmap _blueback;
private Bitmap _bluefront;
private Bitmap _blueleft;
private Bitmap _blueright;
private Bitmap _greenback;
private Bitmap _greenfront;
private Bitmap _greenleft;
private Bitmap _greenright;
private Bitmap _redfront;
private Bitmap _redback;
private Bitmap _redleft;
private Bitmap _redright;
private Bitmap _robot1;
private Bitmap _forest;
private Bitmap _grass;
private Bitmap _mountain;
private Bitmap _tree;
@SuppressWarnings("unused")
private boolean _mapLoaded = false;
private int _viewX = 0;
private int _viewY = 0;

Map(Context context){
    super(context);
    getHolder().addCallback(this);
    _mapThread = new MapThread(this);
    setFocusable(true);

    _terrain = new int[100][100];
    playerList = new ArrayList<Player>();
    addPlayer(0, 0, 0);

    _blueback = BitmapFactory.decodeResource(context.getResources(), R.drawable.blueback);
    _bluefront = BitmapFactory.decodeResource(context.getResources(), R.drawable.bluefront);
    _blueleft = BitmapFactory.decodeResource(context.getResources(), R.drawable.blueleft);
    _blueright = BitmapFactory.decodeResource(context.getResources(), R.drawable.blueright);
    _greenback = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenback);
    _greenfront = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenfront);
    _greenleft = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenleft);
    _greenright = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenright);
    _redback = BitmapFactory.decodeResource(context.getResources(), R.drawable.redback);
    _redfront = BitmapFactory.decodeResource(context.getResources(), R.drawable.redfront);
    _redleft = BitmapFactory.decodeResource(context.getResources(), R.drawable.redleft);
    _redright = BitmapFactory.decodeResource(context.getResources(), R.drawable.redright);
    _robot1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.robot1);
    _forest = BitmapFactory.decodeResource(context.getResources(), R.drawable.forest);
    _grass = BitmapFactory.decodeResource(context.getResources(), R.drawable.grass);
    _mountain = BitmapFactory.decodeResource(context.getResources(), R.drawable.mountain);
    _tree = BitmapFactory.decodeResource(context.getResources(), R.drawable.tree);

    TouchScreenHandler handler = new TouchScreenHandler(); // This includes 2 other nested-class threads
    this.setOnTouchListener(handler);
}

也许onRetainNonConfigurationInstance()返回的对象的复杂性导致了我的问题?

或者最后,是不是我的Manifest.xml文件中有什么东西(比如活动或应用程序属性)出了问题?

如果需要任何进一步的信息,请让我知道,因为我会经常检查这篇文章,直到我可以通过这条道路上的小颠簸。

PS:我的ADB和我的设备都有这个问题。

PSS:最重要的是,非常感谢这个社区的帮助和支持,因为它是非常感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-29 11:48:21

您确认onRetainNonConfigurationInstance()确实被调用了吗?我看到你在里面展示了一个吐司,但你并没有说它正在展示。(为什么要敬酒,而不仅仅是Log.i()?)

正如文档所说,“此函数纯粹是作为优化而调用的,您不能依赖于它被调用。”你不能相信这真的会发生。事实上,只有当您的活动在前台时发生配置更改时,才会发生这种情况,在这种情况下,框架将立即调用onRetainNonConfigurationInstance()并销毁当前实例,并立即使用保留的对象创建一个新实例。应该没有什么可以阻止您返回的对象出现在新实例中……不过,我猜如果你调用finish()或类似的方法来处理旧的代码,可能就行了。

票数 2
EN

Stack Overflow用户

发布于 2010-09-29 05:34:38

不确定你的问题的解决方案,你的代码还有另一个问题。你不应该使用onRetainNonConfigurationInstance来保存对活动上下文的引用,否则以后会泄露活动。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3817159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档