2010-09-28 88 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

相關問題