2010-09-28 90 views
1
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; 
} 
} 

我打算假设我给这个论坛线程的标题说明了我正在经历的问题非常彻底。我还编辑了代码,尝试保存一个字符串对象,然后通过getLastNonConfigurationInstance()恢复字符串对象,只是为了了解我能在多大程度上为我工作,但它似乎仍然返回null。我没有尝试从onStart()或onRestart()或onResume()调用它,但从我读过的内容来看,它通常只能从onCreate(Bundle)调用。这让我真的很困惑...:/getLastNonConfigurationInstance问题 - 似乎只返回null

我想这可能是一些帮助来了解一些关于我的地图类对象,所以这里的(一些),从它的代码:

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()返回的Object的复杂性中存在一些可能会导致我的问题的东西?

最后还是在我的Manifest.xml文件中存在某些问题(如Activity或Application属性)?

如果需要更多信息,请让我知道,因为我会经常检查这个帖子,直到我能在路上经过这个小颠簸。
PS:我对亚行和我的设备都有这个问题。
PSS:最重要的是,非常感谢这个社区的帮助和支持,因为它非常感谢! :D

+0

你如何测试你的'onRetainNonConfigurationInstance()'的行为? – CommonsWare 2010-09-28 21:43:38

+0

if(getLastNonConfigurationInstance()== null){ 是否正确? – sduffy89 2010-09-28 22:45:54

回答

2

您是否确定onRetainNonConfigurationInstance()实际上被称为?我看到你有一个敬酒被显示在里面,但你实际上并没有说它正在显示。 (以及为什么烤面包而不是仅仅是一个Log.i()?)

正如文档所言,“这个函数纯粹称为优化,而且你不能依赖它被调用。”你不能依赖这个实际发生的事情。 只有它发生的时间实际上是当配置发生变化而您的活动处于前台时,在这种情况下,框架将一举唤醒onRetainNonConfigurationInstance()并销毁当前实例并立即创建一个带有保留的新实例对象可用于它。应该有几乎没有什么可以做,这将阻止你返回的对象出现在新的实例中......虽然我猜如果你打电话给finish()或者这样的旧的,可能会这样做。

1

不确定您的问题的解决方案是否存在代码的另一个问题。您不应该使用onRetainNonConfigurationInstance来处理任何类似于该活动上下文的引用,否则您将在稍后泄漏该活动。

+0

我只需要在返回到任务堆栈顶部之后维护状态。我应该考虑让这个android服务类型的应用程序呢?还是应该将所有特定需要的数据和对象压缩成一个ArrayList对象并使用它,并尝试使onRetainNonConfigurationInstance正常工作?顺便说一句,有没有什么常见的情况下onRetainNonConfigurationInstance不能正常工作?我会不会...... – sduffy89 2010-09-28 21:49:37

相关问题