Skip to content

Commit

Permalink
Merge pull request #2 from bugyun/develop
Browse files Browse the repository at this point in the history
1.0.3
  • Loading branch information
bugyun authored Aug 11, 2019
2 parents 10f151d + 1202b38 commit b31e0ac
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 13 deletions.
2 changes: 1 addition & 1 deletion AvoidOnResultHelper/jcenter.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ publish {
userOrg = 'bugyun' //bintray.com用户名
groupId = 'vip.ruoyun.helper' //jcenter group
artifactId = 'avoid-onresult-helper' //项目名称 name
publishVersion = '1.0.1'//版本号 version
publishVersion = '1.0.3'//版本号 version
desc = 'Android 避免 onActivityResult 和 onRequestPermissionsResult 烦恼requestCode的问题,帮助快速开发~~'//描述
website = 'https://github.com/bugyun/AvoidOnResultHelper' // github 网址
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import android.support.v4.app.Fragment;
import android.util.SparseArray;

import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;

/**
* Created by ruoyun on 2019-07-16.
* Author:若云
Expand All @@ -16,13 +20,22 @@
*/
public class AvoidOnResultFragment extends Fragment {

//生命周期状态
public static final int INITIALIZING = 0; // Not yet created.
public static final int STOPPED = 3; // Fully created, not started.
public static final int STARTED = 4; // Created and started, not resumed.
public static final int DESTROYED = 6;

private int mState = INITIALIZING;

//默认值
private static int mRequestCodeStart = 65000;
private static int mRequestCodeEnd = 65535;

private static int mRequestCodeCounter = mRequestCodeStart;
private SparseArray<AvoidOnResultHelper.ActivityCallback> mActivityCallbacks = new SparseArray<>();
private SparseArray<AvoidOnResultHelper.PermissionsCallBack> mPermissionsCallbacks = new SparseArray<>();
private Set<LifecycleListener> lifecycleListeners = Collections.newSetFromMap(new WeakHashMap<LifecycleListener, Boolean>());

public AvoidOnResultFragment() {
}
Expand Down Expand Up @@ -63,7 +76,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
* @param permissions
* @param permissionsCallBack
*/
public void requestPermissions(@NonNull String[] permissions, AvoidOnResultHelper.PermissionsCallBack permissionsCallBack) {
public void requestPermissions(@NonNull String[] permissions, @NonNull AvoidOnResultHelper.PermissionsCallBack permissionsCallBack) {
checkRequestCodeCounter();
mRequestCodeCounter++;
mPermissionsCallbacks.append(mRequestCodeCounter, permissionsCallBack);
Expand All @@ -80,11 +93,64 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}

/**
* 生命周期
*/
public void addLifecycleListener(@NonNull LifecycleListener listener, boolean isSticky) {
lifecycleListeners.add(listener);
if (isSticky) {
switch (mState) {
case STARTED:
listener.onStart();
break;
case STOPPED:
listener.onStop();
break;
case DESTROYED:
listener.onDestroy();
break;
default:
break;
}
}
}

/**
* @param listener
*/
public void removeLifecycleListener(@NonNull LifecycleListener listener) {
lifecycleListeners.remove(listener);
}

@Override
public void onStart() {
super.onStart();
mState = STARTED;
for (LifecycleListener lifecycleListener : lifecycleListeners) {
lifecycleListener.onStart();
}
}

@Override
public void onStop() {
super.onStop();
mState = STOPPED;
for (LifecycleListener lifecycleListener : lifecycleListeners) {
lifecycleListener.onStop();
}
}

@Override
public void onDestroy() {
super.onDestroy();
mState = DESTROYED;
for (LifecycleListener lifecycleListener : lifecycleListeners) {
lifecycleListener.onDestroy();
}
lifecycleListeners.clear();
lifecycleListeners = null;
mActivityCallbacks = null;
mPermissionsCallbacks = null;
super.onDestroy();
}

private void checkRequestCodeCounter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
Expand All @@ -26,7 +27,7 @@ private AvoidOnResultHelper() {
* @param start 默认 65000
* @param end 默认 65535
*/
public static void setRequestCodeRange(int start, int end) {
public static void setRequestCodeRange(@IntRange(from = 0, to = 65535) int start, @IntRange(from = 0, to = 65535) int end) {
if (start >= end) {
Log.e(TAG, "start(" + start + ") must less than end(" + end + ") , Now use the default values !");
return;
Expand All @@ -53,11 +54,11 @@ private static AvoidOnResultFragment getAvoidOnResultFragment(FragmentActivity a
* @param intent
* @param callback
*/
public static void startActivityForResult(FragmentActivity activity, Intent intent, ActivityCallback callback) {
public static void startActivityForResult(@NonNull FragmentActivity activity, @NonNull Intent intent, @NonNull ActivityCallback callback) {
startActivityForResult(activity, intent, null, callback);
}

public static void startActivityForResult(FragmentActivity activity, Intent intent, Bundle options, ActivityCallback callback) {
public static void startActivityForResult(@NonNull FragmentActivity activity, @NonNull Intent intent, Bundle options, @NonNull ActivityCallback callback) {
AvoidOnResultFragment avoidOnResultFragment = getAvoidOnResultFragment(activity);
avoidOnResultFragment.startActivityForResult(intent, options, callback);
}
Expand All @@ -73,7 +74,7 @@ public interface ActivityCallback {
* @param permissions
* @param permissionsCallBack
*/
public static void requestPermissions(FragmentActivity activity, String[] permissions, PermissionsCallBack permissionsCallBack) {
public static void requestPermissions(@NonNull FragmentActivity activity, @NonNull String[] permissions, @NonNull PermissionsCallBack permissionsCallBack) {
AvoidOnResultFragment avoidOnResultFragment = getAvoidOnResultFragment(activity);
avoidOnResultFragment.requestPermissions(permissions, permissionsCallBack);
}
Expand All @@ -83,4 +84,27 @@ public interface PermissionsCallBack {
}


/**
* 生命周期,非粘性,注册的时候不调用回调函数
*/
public static void addLifecycleListener(@NonNull FragmentActivity activity, @NonNull LifecycleListener listener) {
addLifecycleListener(activity, listener, false);
}

/**
* 粘性,注册的时候调用回调函数
*
* @param activity
* @param listener
*/
public static void addLifecycleListener(@NonNull FragmentActivity activity, @NonNull LifecycleListener listener, boolean isSticky) {
AvoidOnResultFragment avoidOnResultFragment = getAvoidOnResultFragment(activity);
avoidOnResultFragment.addLifecycleListener(listener, isSticky);
}


public static void removeLifecycleListener(@NonNull FragmentActivity activity, @NonNull LifecycleListener listener) {
AvoidOnResultFragment avoidOnResultFragment = getAvoidOnResultFragment(activity);
avoidOnResultFragment.removeLifecycleListener(listener);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package vip.ruoyun.helper.avoid;

/**
* Created by ruoyun on 2019-08-11.
* Author:若云
* Mail:[email protected]
* Depiction:
*/
public interface LifecycleListener {

/**
* 开始
*/
void onStart();

/**
* 结束
*/
void onStop();

/**
* 销毁
*/
void onDestroy();

class LifecycleListenerWrapper implements LifecycleListener {

@Override
public void onStart() {

}

@Override
public void onStop() {

}

@Override
public void onDestroy() {

}
}
}
3 changes: 0 additions & 3 deletions AvoidOnResultHelper/src/main/res/values/strings.xml

This file was deleted.

37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

```java
dependencies {
implementation 'vip.ruoyun.helper:avoid-onresult-helper:1.0.1'
implementation 'vip.ruoyun.helper:avoid-onresult-helper:1.0.3'
}
```

Expand Down Expand Up @@ -51,6 +51,41 @@ AvoidOnResultHelper.requestPermissions(this, permissions, new AvoidOnResultHelpe
AvoidOnResultHelper.setRequestCodeRange(65000, 65535);
```

## 生命周期
监听当前 activity 的生命周期.
```java
public interface LifecycleListener {

void onStart();//开始

void onStop();//结束

void onDestroy();//销毁
}
```
LifecycleListenerWrapper 是此接口的空实现.

使用
```java
//添加监听事件
LifecycleListener lifecycleListener = new LifecycleListener.LifecycleListenerWrapper() {
@Override
public void onStart() {
}
};
//添加监听事件
AvoidOnResultHelper.addLifecycleListener(this, lifecycleListener);

//粘性通知,当第一次添加的时候,就执行相应的回调方法
AvoidOnResultHelper.addLifecycleListener(this, lifecycleListener, true);
```

可选操作
```java
//移除监听事件,默认可以不用移除lifecycleListener,除非在当前界面存在时候,想要不让某些类监听,那么可以手动进行移除(removeLifecycleListener)
AvoidOnResultHelper.removeLifecycleListener(this, lifecycleListener);
```

## 原理

### 问题
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation project(path: ':AvoidOnResultHelper')
// implementation 'vip.ruoyun.helper:avoid-onresult-helper:1.0.1'
// implementation 'vip.ruoyun.helper:avoid-onresult-helper:1.0.2'

}
28 changes: 27 additions & 1 deletion app/src/main/java/vip/ruoyun/avoidonresult/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Arrays;

import vip.ruoyun.helper.avoid.AvoidOnResultHelper;
import vip.ruoyun.helper.avoid.LifecycleListener;


public class MainActivity extends AppCompatActivity {
Expand All @@ -32,7 +33,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AvoidOnResultHelper.setRequestCodeRange(200, 200);
AvoidOnResultHelper.setRequestCodeRange(65535, 200);


Button mButton = findViewById(R.id.mButton);
Expand Down Expand Up @@ -65,6 +66,31 @@ public void onRequestPermissionsResult(@NonNull String[] permissions, @NonNull i
OneFragment fragment = new OneFragment();
fragmentTransaction.replace(R.id.mFrameLayout, fragment, "OneFragment");
fragmentTransaction.commit();

LifecycleListener lifecycleListener = new LifecycleListener() {


@Override
public void onStart() {
}



@Override
public void onStop() {

}

@Override
public void onDestroy() {

}
};


AvoidOnResultHelper.addLifecycleListener(this, lifecycleListener);
AvoidOnResultHelper.addLifecycleListener(this, lifecycleListener, true);
AvoidOnResultHelper.removeLifecycleListener(this, lifecycleListener);
}

private void test() {
Expand Down

0 comments on commit b31e0ac

Please sign in to comment.