本篇文章已受權微信公衆號 guolin_blog (郭霖)獨家發佈javascript
Fragment,簡稱碎片,能夠簡單地認爲它就是一個「控件」,更加具體一點就是「View控制器」。它自身有生命週期。在開發中,咱們常常用到,再熟悉不過了。然而,Fragment 的一些巧妙引用,不知道你是否瞭解過?java
這篇文章主要講解如下內容git
固然,這些封裝,網上都有相應的開源庫了, RxPermission, EasyPermision, RxActivityReslut 等,這裏講解如何封裝,主要是讓你們瞭解背後的原理,加深理解。想要成爲一名優秀的工程師(程序猿),光停留在表面是遠遠不夠的,咱們要多讀源碼,理解背後的原理。哈哈,廢話很少說,開始進入正文。github
Android 6.0 動態權限機制,你們再熟悉不過了,若是咱們沒有對其進行封裝,那咱們每一次在申請權限的時候,大概須要如下幾步:緩存
這裏咱們已撥打電話爲例子進行講解微信
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CALL_PHONE},
10);
} else{
callPhone();
}
複製代碼
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == 10) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
callPhone();
} else {
// Permission Denied
Toast.makeText(TestResultActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
}
複製代碼
看到這樣的代碼你會不會很煩,每次涉及權限操做的時候,都要寫這樣一堆這樣重複的代碼,枯燥,且不少代碼邏輯會耦合在 Activity 中,不方便維護。那有沒有辦法,將這些繁瑣的步驟封裝起來呢,答案是有的。dom
如今網上的作法通常有如下幾種ide
這裏咱們使用 Fragment 進行封裝。ui
咱們知道, Fragment 通常依賴於 Activity 存活,而且生命週期跟 Activity 差很少,所以,咱們進行權限申請的時候,能夠利用透明的 Fragment 進行申請,在裏面處理完以後,再進行相應的回調。this
首先,咱們先來看一下代理 Fragment EachPermissionFragment 是怎麼封裝的?
public class EachPermissionFragment extends Fragment {
private SparseArray<IPermissionListenerWrap.IPermissionListener> mCallbacks = new SparseArray<>();
private SparseArray<IPermissionListenerWrap.IEachPermissionListener> mEachCallbacks = new SparseArray<>();
private Random mCodeGenerator = new Random();
private FragmentActivity mActivity;
public EachPermissionFragment() {
// Required empty public constructor
}
public static EachPermissionFragment newInstance() {
return new EachPermissionFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設置爲 true,表示 configuration change 的時候,fragment 實例不會背從新建立
setRetainInstance(true);
mActivity = getActivity();
}
public void requestPermissions(@NonNull String[] permissions, IPermissionListenerWrap.IPermissionListener callback) {
int requestCode = makeRequestCode();
mCallbacks.put(requestCode, callback);
requestPermissions(permissions, requestCode);
}
public void requestEachPermissions(@NonNull String[] permissions, IPermissionListenerWrap.IEachPermissionListener callback) {
int requestCode = makeRequestCode();
mEachCallbacks.put(requestCode, callback);
requestPermissions(permissions, requestCode);
}
/** * 隨機生成惟一的requestCode,最多嘗試10次 * * @return */
private int makeRequestCode() {
int requestCode;
int tryCount = 0;
do {
requestCode = mCodeGenerator.nextInt(0x0000FFFF);
tryCount++;
} while (mCallbacks.indexOfKey(requestCode) >= 0 && tryCount < 10);
return requestCode;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
handlePermissionCallBack(requestCode, grantResults);
handleEachPermissionCallBack(requestCode, permissions, grantResults);
}
private void handlePermissionCallBack(int requestCode, @NonNull int[] grantResults) {
IPermissionListenerWrap.IPermissionListener callback = mCallbacks.get(requestCode);
mCallbacks.remove(requestCode);
if (callback == null) {
return;
}
boolean allGranted = false;
int length = grantResults.length;
for (int i = 0; i < length; i++) {
int grantResult = grantResults[i];
if (grantResult != PackageManager.PERMISSION_GRANTED) {
allGranted = false;
break;
}
allGranted = true;
}
if (allGranted) {
callback.onAccepted(true);
} else {
callback.onAccepted(false);
}
}
private void handleEachPermissionCallBack(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
IPermissionListenerWrap.IEachPermissionListener eachCallback = mEachCallbacks.get(requestCode);
if (eachCallback == null) {
return;
}
mEachCallbacks.remove(requestCode);
int length = grantResults.length;
for (int i = 0; i < length; i++) {
int grantResult = grantResults[i];
Permission permission;
String name = permissions[i];
if (grantResult == PackageManager.PERMISSION_GRANTED) {
permission = new Permission(name, true);
eachCallback.onAccepted(permission);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, name)) {
permission = new Permission(name, false, true);
} else {
permission = new Permission(name, false, false);
}
eachCallback.onAccepted(permission);
}
}
}
}
複製代碼
咱們先來看一下它的 onCreate 方法,在 onCreate 方法裏面,咱們調用了 setRetainInstance 方法。
setRetainInstance(boolean retain)
Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change)
表示當 Activity 從新建立的時候, fragment 實例是否會被從新建立(好比橫豎屏切換),設置爲 true,表示 configuration change 的時候,fragment 實例不會背從新建立,這樣,有一個好處,即 configuration 變化的時候,咱們不須要再作額外的處理。所以, fragment 該方法也經常被用來處理 Activity re-creation 時候數據的保存,是否是又 get 到了什麼?
接着咱們來看 requestEachPermissions 方法
而後看 onRequestPermissionsResult 方法
這裏咱們主要關注 handleEachPermissionCallBack(requestCode, permissions, grantResults); 方法, handlePermissionCallBack 方法思路也是相似的
public class Permission {
public final String name;
public final boolean granted;
/** * false 選擇了 Don’t ask again */
public final boolean shouldShowRequestPermissionRationale;
}
複製代碼
Permission 含有三個字段,name 表示權限的名字,granted 表示是否受權,shouldShowRequestPermissionRationale 表示是否勾選了 Don’t ask again(即再也不詢問),一般咱們的作法是若勾選了再也不詢問,咱們須要引導用戶,跳轉到相應的 Setting 頁面。
大體代碼以下
/** * 打開設置頁面打開權限 * * @param context */
public static void startSettingActivity(@NonNull Activity context) {
try {
Intent intent =
new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" +
context.getPackageName()));
intent.addCategory(Intent.CATEGORY_DEFAULT);
context.startActivityForResult(intent, 10); //這裏的requestCode和onActivityResult中requestCode要一致
} catch (Exception e) {
e.printStackTrace();
}
}
複製代碼
封裝完成以後,咱們只須要調用如下方法便可,簡單,方便,快捷。
private void requestPermission(final String[] permissions) {
PermissionsHelper.init(MainActivity.this).requestEachPermissions(permissions, new IPermissionListenerWrap.IEachPermissionListener() {
@Override
public void onAccepted(Permission permission) {
show(permission);
}
@Override
public void onException(Throwable throwable) {
}
});
}
複製代碼
OK,咱們再來梳理一下流程
咱們先來看一下沒封裝以前 onActivityresult 的處理方式
咱們先來看下正常狀況下啓動 Activity 和接收回調信息的方式:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 啓動Activity
startActivityForResult(new Intent(this, TestActivity.class), 10);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 接收Activity回調
if (requestCode == 10) {
// 處理回調信息
}
}
複製代碼
這樣在簡單頁面下,看起來沒什麼問題,也簡單易懂。但實際上,這種方式會存在一些侷限
同理,咱們也能夠跟上面的權限封裝同樣,用空白的 fragment 進行代理,進行封裝。封裝後的代碼調用以下。
Intent intent = new Intent(MainActivity.this, TestResultActivity.class);
ActivityResultHelper.init(MainActivity.this).startActivityForResult(intent, new ActivityResultHelper.Callback() {
@Override
public void onActivityResult(int resultCode, Intent data) {
String result = data.getStringExtra(TestResultActivity.KEY_RESULT);
show(" resultCode = " + resultCode + " result = " + result);
}
});
複製代碼
思路以下
當咱們想發起 startActivityresult 的時候,使用代理 Fragment 進行代理,調用startActivityForResult 方法,它須要兩個參數, intent, 和 requestCode, intent 表明要跳轉的動做, requestCode 用來區分是那個動做。這裏,爲了簡化,咱們隨機生成 requestCode ,並緩存起來,下次申請的時候,再隨機申請,確保不會重複。
在 onActivityresult 裏面根據 requestCode 找到相應的 callback,並進行相應的回調。
中轉的 Fragment RouterFragment 核心代碼以下
public class RouterFragment extends Fragment {
private SparseArray<ActivityResultHelper.Callback> mCallbacks = new SparseArray<>();
private Random mCodeGenerator = new Random();
public RouterFragment() {
// Required empty public constructor
}
public static RouterFragment newInstance() {
return new RouterFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
public void startActivityForResult(Intent intent, ActivityResultHelper.Callback callback) {
int requestCode = makeRequestCode();
mCallbacks.put(requestCode, callback);
startActivityForResult(intent, requestCode);
}
/** * 隨機生成惟一的requestCode,最多嘗試10次 * * @return */
private int makeRequestCode() {
int requestCode;
int tryCount = 0;
do {
requestCode = mCodeGenerator.nextInt(0x0000FFFF);
tryCount++;
} while (mCallbacks.indexOfKey(requestCode) >= 0 && tryCount < 10);
return requestCode;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ActivityResultHelper.Callback callback = mCallbacks.get(requestCode);
mCallbacks.remove(requestCode);
if (callback != null) {
callback.onActivityResult(resultCode, data);
}
}
}
複製代碼
其餘的代碼這裏就不貼了,有興趣的請自行到 GitHub 上面查看
GitHub 地址 github.com/gdutxiaoxu/…
看了上面 Fragment 的妙用,封裝權限,處理 onActivityResult,你是否想到了什麼?
你呢, Fragment 的妙用你還知道哪些,歡迎留言評論。
Android 技術人,一位不羈的碼農,撩天撩地撩技術,期待你的參與。