這裏是所有回調方法的替代解決方案,它們可能都受到與活動週期相同的未定義事件順序行爲的影響。除非您要檢查每次回調的所有android代碼,您可以使用它們來確定原始觸發器和控制實現的人員,並希望將來代碼庫不會更改,但是真的可以說明,回調函數之間的事件順序並且活動生命週期事件可以得到保證。
現在,爲了開發目的,訂單的這些交互通常可以稱爲未定義的行爲。
所以最好是始終正確處理這個未定義的行爲,這樣 它將永遠不會成爲一個問題,通過確保訂單是定義的行爲。
我的索尼XPERIA例如,睡眠,週期我目前的應用程序,通過破壞應用程序,然後重新啓動它,並把它進入暫停狀態,信不信由你。
測試谷歌在SDK中提供多少事件排序行爲作爲特殊的測試構建主機環境實現我不知道,但他們肯定需要努力確保,事件訂單的行爲都被鎖定在這件事上相當嚴格。
https://code.google.com/p/android/issues/detail?id=214171&sort=-opened&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened
進口android.util.Log; import android.util.SparseArray;
/** * 通過對2016年6月24日woliver創建。 * * Android的主機環境,決定了在OnCreate,在onStart,的onResume,在onPause,的onStop,onDestory, 一個Activity生命週期*,其中由我們需要釋放內存並處理其他應用程序使用。 *當恢復時,我們有時需要重新綁定和激活這些項目與其他對象。 *典型地,這些其它的目的提供從主機環境,其提供 *一個onCreated和的onDestroy,我們可以在其中僅結合從OnCreated該對象的回調方法和和鬆散 *出綁定onDestory。 *這些類型的回調方法,shedual時間運行是控制器通過我們的主機環境 *和他們的無人保證了活動的生命週期的執行行爲/順序和這些回調方法 *保持一致。 *爲發展宗旨執行的相互作用和順序可以在技術上被稱爲未定義 *,因爲它是由主機執行實施者,三星,索尼,HTC。 * *請參閱以下開發人員文檔:https://developer.android.com/reference/android/app/Activity.html * Quote: *如果一項活動被其他活動完全遮擋,則會停止。它仍然保留所有的狀態 *和成員信息,但是,它不再對用戶可見的所以它的窗口是 *隱藏它通常會被系統的其他地方時,需要記憶被殺害。 * EndQuato: * *如果活動沒有隱藏,那麼系統將會調用主機 *系統調用的任何回調,例如OnCreate和OnDestory方法接口SurfaceView回調。 *這意味着,你將不得不停止已綁定到SurfaceView對象,如相機 *在暫停和永遠不會重新綁定對象爲在OnCreate回調將不會被調用。 * */
public abstract class WaitAllActiveExecuter<Size>
{
private SparseArray<Boolean> mReferancesState = null;
// Use a dictionary and not just a counter, as hosted code
// environment implementer may make a mistake and then may double executes things.
private int mAllActiveCount = 0;
private String mContextStr;
public WaitAllActiveExecuter(String contextStr, int... identifiers)
{
mReferancesState = new SparseArray<Boolean>(identifiers.length);
mContextStr = contextStr;
for (int i = 0; i < identifiers.length; i++)
mReferancesState.put(identifiers[i], false);
}
public void ActiveState(int identifier)
{
Boolean state = mReferancesState.get(identifier);
if (state == null)
{
// Typically panic here referance was not registered here.
throw new IllegalStateException(mContextStr + "ActiveState: Identifier not found '" + identifier + "'");
}
else if(state == false){
mReferancesState.put(identifier, true);
mAllActiveCount++;
if (mAllActiveCount == mReferancesState.size())
RunActive();
}
else
{
Log.e(mContextStr, "ActivateState: called to many times for identifier '" + identifier + "'");
// Typically panic here and output a log message.
}
}
public void DeactiveState(int identifier)
{
Boolean state = mReferancesState.get(identifier);
if (state == null)
{
// Typically panic here referance was not registered here.
throw new IllegalStateException(mContextStr + "DeActiveState: Identifier not found '" + identifier + "'");
}
else if(state == true){
if (mAllActiveCount == mReferancesState.size())
RunDeActive();
mReferancesState.put(identifier, false);
mAllActiveCount--;
}
else
{
Log.e(mContextStr,"DeActiveState: State called to many times for identifier'" + identifier + "'");
// Typically panic here and output a log message.
}
}
private void RunActive()
{
Log.v(mContextStr, "Executing Activate");
ExecuterActive();
}
private void RunDeActive()
{
Log.v(mContextStr, "Executing DeActivate");
ExecuterDeActive();
}
abstract public void ExecuterActive();
abstract public void ExecuterDeActive();
}
實施和使用類的,其與機器人或主機環境 實施者的未定義行爲涉及的實施例。
private final int mBCTSV_SurfaceViewIdentifier = 1;
private final int mBCTSV_CameraIdentifier = 2;
private WaitAllActiveExecuter mBindCameraToSurfaceView =
new WaitAllActiveExecuter("BindCameraToSurfaceViewe", new int[]{mBCTSV_SurfaceViewIdentifier, mBCTSV_CameraIdentifier})
{
@Override
public void ExecuterActive() {
// Open a handle to the camera, if not open yet and the SurfaceView is already intialized.
if (mCamera == null)
{
mCamera = Camera.open(mCameraIDUsed);
if (mCamera == null)
throw new RuntimeException("Camera could not open");
// Look at reducing the calls in the following two methods, some this is unessary.
setDefaultCameraParameters(mCamera);
setPreviewSizesForCameraFromSurfaceHolder(getSurfaceHolderForCameraPreview());
}
// Bind the Camera to the SurfaceView.
try {
mCamera.startPreview();
mCamera.setPreviewDisplay(getSurfaceHolderForCameraPreview());
} catch (IOException e) {
e.printStackTrace();
ExecuterDeActive();
throw new RuntimeException("Camera preview could not be set");
}
}
@Override
public void ExecuterDeActive() {
if (mCamera != null)
{
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
};
@Override
protected void onPause() {
mBindCameraToSurfaceView.DeactiveState(mBCTSV_CameraIdentifier);
Log.v(LOG_TAG, "Activity Paused - After Super");
}
@Override
public void onResume() {
mBindCameraToSurfaceView.ActiveState(mBCTSV_CameraIdentifier);
}
private class SurfaceHolderCallback implements SurfaceHolder.Callback
{
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.v(LOG_TAG, "Surface Changed");
}
public void surfaceCreated(SurfaceHolder surfaceHolder) {
Log.v(LOG_TAG, "Surface Created");
mBindCameraToSurfaceView.ActiveState(mBCTSV_SurfaceViewIdentifier);
}
public void surfaceDestroyed(SurfaceHolder arg0) {
Log.v(LOG_TAG, "Surface Destoryed");
mBindCameraToSurfaceView.DeactiveState(mBCTSV_SurfaceViewIdentifier);
}
}
您正在開發哪種android Plaform/API級別? – FerranB 2012-08-26 16:58:24