描述:當(dāng)app攝像頭權(quán)限打開后,運(yùn)行中的app和單例中的數(shù)據(jù)正常;切換到設(shè)置頁面關(guān)閉攝像頭權(quán)限后;再切換到app發(fā)生的變化
結(jié)果: activity會(huì)重新繪制,activity歷史棧會(huì)保留,但是回退到歷史棧的activity會(huì)重新從onCreate(***)開始;且單例中的數(shù)據(jù)被重置,例如:int 數(shù)據(jù)被重置為0,對(duì)象被重置為null
單例代碼
public class CountManager {private CountManager() {}
public static CountManager getInstance() {return SingleHolder.instance;
}
private static class SingleHolder {private static final CountManager instance = new CountManager();
}
private int mCount = 0;
public void addCount() {mCount++;
}
public int getCount() {return mCount;
}
}
第一個(gè)activity
public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity-1";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
CountManager.getInstance().addCount(); //單例中數(shù)據(jù)增加
findViewById(R.id.btn_second_click).setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
Log.d(TAG, "onCreate: ");
}
@Override
protected void onResume() {super.onResume();
Log.d(TAG, "onResume: " + CountManager.getInstance().getCount());
}
@Override
protected void onPause() {super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onDestroy() {super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
第二個(gè)activity
public class SecondActivity extends AppCompatActivity {private static final String TAG = "MainActivity-Second";
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CountManager.getInstance().addCount();
if (savedInstanceState == null) {getSupportFragmentManager().beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow();
}
Log.d(TAG, "onCreate: ");
}
@Override
protected void onResume() {super.onResume();
Log.d(TAG, "onResume: "+ CountManager.getInstance().getCount());
}
@Override
protected void onPause() {super.onPause();
Log.d(TAG, "onPause: ");
}
@Override
protected void onStop() {super.onStop();
Log.d(TAG, "onStop: ");
}
@Override
protected void onDestroy() {super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
備注:記得app增加攝像頭權(quán)限
運(yùn)行結(jié)果1、啟動(dòng)app后,在MainActivity,點(diǎn)擊跳轉(zhuǎn)到SecondActivity
D onCreate:
15:28:09.177 com.xiaoma.testc D onResume: 1
15:28:11.663 com.xiaoma.testc D onPause:
15:28:11.708 com.xiaoma.testc D onCreate:
15:28:11.751 com.xiaoma.testc D onResume: 2
2、切換到桌面,攝像頭權(quán)限進(jìn)行關(guān)閉操作或者打開后再關(guān)閉
15:31:44.956 com.xiaoma.testc D onPause:
15:31:44.989 com.xiaoma.testc D onStop:
---------------------------- PROCESS ENDED (31143) for package com.xiaoma.testc ----------------------------
可以看到進(jìn)程關(guān)閉了的信息
3、再次點(diǎn)擊app,查看里面的數(shù)據(jù),然后進(jìn)行回退歷史棧
---------------------------- PROCESS STARTED (32348) for package com.xiaoma.testc ----------------------------
15:32:57.135 com.xiaoma.testc D onCreate:
15:32:57.170 com.xiaoma.testc D onResume: 1
15:33:13.688 com.xiaoma.testc D onPause:
//返回操作后
15:33:13.751 com.xiaoma.testc D onCreate:
15:33:13.755 com.xiaoma.testc D onResume: 2
15:33:14.162 com.xiaoma.testc D onStop:
15:33:14.167 com.xiaoma.testc D onDestroy:
可以看到重啟啟動(dòng)了進(jìn)程,且歷史棧存在
總結(jié)對(duì)于開發(fā)SDK的,由于需要宿主傳遞接口回調(diào),會(huì)導(dǎo)致回調(diào)為null,所以在頁面需要進(jìn)行回調(diào)判斷,進(jìn)行相關(guān)的業(yè)務(wù)處理。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧