这是所有回调方法的替代解决方案,这些方法可能都具有相同的未定义事件顺序行为(活动周期)。除非您要检查每个用于确定原始触发器和控制实现的回调的android代码,否则希望代码库将来不会更改,除非您能真正声明,回调之间的事件顺序活动生命周期事件可以得到保证。
现在,出于开发目的,这些顺序交互通常可以称为未定义行为。
因此,最好是始终正确处理此未定义的行为,这样一来,通过确保已定义订单的行为就永远不会成为问题。
例如,我的Sony Xperia在睡眠时会破坏我的当前应用程序,方法是销毁该应用程序,然后重新启动该应用程序并将其置于暂停状态,无论您是否相信。
google在其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; 导入android.util.SparseArray;
/ ** *由woliver在2016/06/24创建。* * Android主机环境规定了OnCreate,onStart,onResume,onPause,onStop,onDestory等的活动生命周期,*在这些情况下,我们需要释放内存和句柄供其他应用程序使用。*恢复时,有时需要重新绑定这些项目并将其与其他对象一起激活。通常,这些其他对象从主机环境提供回调方法,这些方法提供onCreated和onDestroy,在这些方法中,我们只能从OnCreated绑定到该对象,然后松开绑定onDestory。*这些类型的回调方法,运行所需的时间是我们的宿主环境*,并且不能保证活动生命周期的行为/执行顺序和这些回调方法*保持一致。*为了开发的目的,交互作用和执行顺序在技术上可以称为undefined *,因为它取决于主机实现实现者,三星,索尼,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;
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)
{
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 + "'");
}
}
public void DeactiveState(int identifier)
{
Boolean state = mReferancesState.get(identifier);
if (state == null)
{
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 + "'");
}
}
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();
}
类的实现和使用示例,该类处理android主机环境实现者的行为或未定义的行为。
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() {
if (mCamera == null)
{
mCamera = Camera.open(mCameraIDUsed);
if (mCamera == null)
throw new RuntimeException("Camera could not open");
setDefaultCameraParameters(mCamera);
setPreviewSizesForCameraFromSurfaceHolder(getSurfaceHolderForCameraPreview());
}
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);
}
}