├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── gradle.xml ├── markdown-exported-files.xml ├── markdown-navigator.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── SimpleRxBus ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── simplerxbus │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── simplerxbus │ │ │ ├── bus │ │ │ ├── RxBusMessage.java │ │ │ ├── RxBusReceiver.java │ │ │ ├── RxBusUtils.java │ │ │ └── RxSimpleBus.java │ │ │ ├── lifecycle │ │ │ ├── EmptyActivityLifecycleCallbacks.java │ │ │ └── HolderLifeFragment.java │ │ │ └── observer │ │ │ └── RxResourceObserver.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── example │ └── simplerxbus │ └── ExampleUnitTest.java ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── laddingwu │ │ └── rxbusapplication │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── laddingwu │ │ │ └── rxbusapplication │ │ │ ├── MainActivity.java │ │ │ ├── SecondActivity.java │ │ │ ├── Tag.java │ │ │ ├── ThridActivity.java │ │ │ ├── fragments │ │ │ ├── FristFragment.java │ │ │ └── SecondFragment.java │ │ │ └── model │ │ │ └── ActivityToFragmentsBean.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_second.xml │ │ ├── activity_thrid.xml │ │ └── fragment_plus_one.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── laddingwu │ └── rxbusapplication │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | /.idea 12 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/markdown-exported-files.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 36 | 37 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 ladingwu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimpleRxBus 2 | > **一个更加简单易用的RxBus** 3 | 4 | - 使用简介 5 | - 集成 6 | ``` 7 | //请确保原来项目中已经引入了RxJava2.0+的依赖,如没有,请引入 8 | implementation 'com.ladingwu.library:SimpleRxBus:0.3' 9 | // 需要v7的support包,如果原项目中已经存在,则不必引入 10 | implementation 'com.android.support:appcompat-v7:28.0.0' 11 | 12 | 13 | ``` 14 | - 发送普通事件 15 | ``` 16 | RxBusUtils.post("filter_message",mesage); 17 | ``` 18 | 19 | - 接收事件(自动取消订阅) 20 | ``` 21 | // in Fragment or FragmentActivity 22 | RxBusUtils.receive(this,"filter_message", new RxBusReceiver() { 23 | @Override 24 | public void receive(Object message) { 25 | // handle this 26 | } 27 | }); 28 | ``` 29 | 30 | - 发送粘性事件 31 | ``` 32 | RxBusUtils.postSticky("filter_sticky_message",message); 33 | ``` 34 | - 接收粘性事件(自动取消订阅) 35 | ``` 36 | // in Fragment or FragmentActivity 37 | RxBusUtils.receiveSticky(this,"filter_message", new RxBusReceiver() { 38 | @Override 39 | public void receive(Object message) { 40 | // handle this 41 | } 42 | }); 43 | ``` 44 | 45 | - 特殊情况 46 | ``` 47 | //如果无法拿到Fragment/FragmentActivity的实例,则接收事件的时候,需要自行处理取消注册的工作 48 | Disposable disposable = RxBusUtils.receive("filter", new RxBusReceiver() { 49 | @Override 50 | public void receive(Object data) { 51 | // handle this 52 | } 53 | }); 54 | 55 | // 在合适的时机取消注册 56 | if (disposable != null && !disposable.isDisposed()) { 57 | disposable.dispose(); 58 | } 59 | ``` 60 | 61 | - SimpleRxBus的优点 62 | - 简单,好用,支持普通事件/粘性事件的收发 63 | - 无需显式的调用注册和取消注册接口,框架会自动注册,并且根据生命周期会自动取消注册 64 | - 即插即用,无论是新项目还是老项目,都不需要做额外的处理,在这一点上,对老项目尤为友好 -------------------------------------------------------------------------------- /SimpleRxBus/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /SimpleRxBus/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.novoda.bintray-release' 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | minSdkVersion 14 7 | targetSdkVersion 28 8 | versionCode 1 9 | versionName "1.0" 10 | 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | 13 | } 14 | 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | 22 | } 23 | 24 | //新添加 25 | publish { 26 | userOrg = 'ladingwu' //在https://bintray.com上注册的用户名 27 | repoName='Maven' 28 | groupId = 'com.ladingwu.library' //jCenter上的路径 29 | artifactId = 'SimpleRxBus' //要上传的library名称 30 | publishVersion = "$library_version" //library的版本号 31 | desc = 'an Simple' //library的简单描述 32 | website = 'https://github.com/ladingwu/SimpleRxBusApplication' //library的开源地址,例如在github上的地址 33 | } 34 | 35 | 36 | dependencies { 37 | implementation fileTree(dir: 'libs', include: ['*.jar']) 38 | 39 | compileOnly 'com.android.support:appcompat-v7:28.0.0' 40 | testImplementation 'junit:junit:4.12' 41 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 42 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 43 | compileOnly "io.reactivex.rxjava2:rxjava:2.1.6" 44 | compileOnly "io.reactivex.rxjava2:rxandroid:2.0.1" 45 | } 46 | -------------------------------------------------------------------------------- /SimpleRxBus/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /SimpleRxBus/src/androidTest/java/com/example/simplerxbus/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.example.simplerxbus; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.example.simplerxbus.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SimpleRxBus/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /SimpleRxBus/src/main/java/com/example/simplerxbus/bus/RxBusMessage.java: -------------------------------------------------------------------------------- 1 | package com.example.simplerxbus.bus; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | /** 6 | * Created by 拉丁吴 on 2018 7 | */ 8 | 9 | class RxBusMessage { 10 | private String type; 11 | private Object msg; 12 | 13 | public RxBusMessage(@NonNull String type, @NonNull Object msg) { 14 | this.type = type; 15 | this.msg = msg; 16 | } 17 | 18 | public String getType() { 19 | return type; 20 | } 21 | 22 | public void setType(String type) { 23 | this.type = type; 24 | } 25 | 26 | public Object getMsg() { 27 | return msg; 28 | } 29 | 30 | public void setMsg(Object msg) { 31 | this.msg = msg; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /SimpleRxBus/src/main/java/com/example/simplerxbus/bus/RxBusReceiver.java: -------------------------------------------------------------------------------- 1 | package com.example.simplerxbus.bus; 2 | 3 | import android.support.annotation.CallSuper; 4 | import android.util.Log; 5 | 6 | import com.example.simplerxbus.observer.RxResourceObserver; 7 | 8 | /** 9 | * Created by 拉丁吴 on 2018 10 | */ 11 | 12 | public abstract class RxBusReceiver extends RxResourceObserver { 13 | 14 | 15 | private String filter; 16 | @Override 17 | public void onNext(T t) { 18 | try { 19 | receive(t); 20 | 21 | } catch (Exception e) { 22 | 23 | } 24 | } 25 | 26 | @CallSuper 27 | @Override 28 | public void onError(Throwable e) { 29 | clearIfNeed(); 30 | } 31 | 32 | @CallSuper 33 | @Override 34 | public void onComplete() { 35 | clearIfNeed(); 36 | } 37 | protected void setCurFilter(String filter){ 38 | this.filter = filter; 39 | } 40 | 41 | public abstract void receive(T data); 42 | private void clearIfNeed(){ 43 | if (filter != null) { 44 | RxSimpleBus.getBus().clearCurMessage(filter); 45 | } 46 | } 47 | 48 | @Override 49 | public void dispose() { 50 | super.dispose(); 51 | clearIfNeed(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /SimpleRxBus/src/main/java/com/example/simplerxbus/bus/RxBusUtils.java: -------------------------------------------------------------------------------- 1 | package com.example.simplerxbus.bus; 2 | 3 | import android.support.v4.app.Fragment; 4 | import android.support.v4.app.FragmentActivity; 5 | 6 | import com.example.simplerxbus.lifecycle.HolderLifeFragment; 7 | 8 | import io.reactivex.disposables.Disposable; 9 | 10 | public class RxBusUtils { 11 | /** 12 | * @param tag 13 | * @param event 14 | * 发送普通事件 15 | */ 16 | public static void post(String tag, Object event) { 17 | RxSimpleBus.getBus().sendMessage(new RxBusMessage(tag, event)); 18 | } 19 | 20 | /** 21 | * 22 | * @param filter 23 | * @param receiver 24 | * @return 25 | */ 26 | public static Disposable receive(String filter, RxBusReceiver receiver) { 27 | return RxSimpleBus.getBus().receiveMessageFrom(filter, receiver); 28 | } 29 | 30 | /** 31 | * 发送粘性事件 32 | * @param tag 33 | * @param event 34 | */ 35 | public static void postSticky(String tag, Object event) { 36 | RxSimpleBus.getBus().sendStickyMessage(new RxBusMessage(tag, event)); 37 | } 38 | 39 | /** 40 | * 41 | * @param filter 42 | * @param receiver 43 | * @return 44 | */ 45 | public static Disposable receiveSticky(String filter, RxBusReceiver receiver) { 46 | receiver.setCurFilter(filter); 47 | return RxSimpleBus.getBus().receiveStickyMessage(filter, receiver); 48 | } 49 | 50 | /** 51 | * 接受普通事件 52 | * 自动取消注册 53 | * @param fragment 54 | * @param filter 55 | * @param receiver 56 | */ 57 | public static void receive(Fragment fragment, String filter, RxBusReceiver receiver) { 58 | Disposable disposable = receive(filter, receiver); 59 | HolderLifeFragment fragment1 = HolderLifeFragment.holderFragmentFor(fragment); 60 | setDispose(fragment1, disposable); 61 | } 62 | /** 63 | * 接受普通事件 64 | * 自动取消注册 65 | * @param activity 66 | * @param filter 67 | * @param receiver 68 | */ 69 | public static void receive(FragmentActivity activity, String filter, RxBusReceiver receiver) { 70 | Disposable disposable = receive(filter, receiver); 71 | HolderLifeFragment fragment1 = HolderLifeFragment.holderFragmentFor(activity); 72 | setDispose(fragment1, disposable); 73 | } 74 | 75 | /** 76 | * 接受粘性事件 77 | * 自动取消注册 78 | * @param fragment 79 | * @param filter 80 | * @param receiver 81 | */ 82 | public static void receiveSticky(Fragment fragment, String filter, RxBusReceiver receiver) { 83 | Disposable disposable = receiveSticky(filter, receiver); 84 | HolderLifeFragment fragment1 = HolderLifeFragment.holderFragmentFor(fragment); 85 | setDispose(fragment1, disposable); 86 | } 87 | 88 | /** 89 | * 90 | * @param activity 91 | * @param filter 92 | * @param receiver 93 | */ 94 | public static void receiveSticky(FragmentActivity activity, String filter, RxBusReceiver receiver) { 95 | Disposable disposable = receiveSticky(filter, receiver); 96 | HolderLifeFragment fragment1 = HolderLifeFragment.holderFragmentFor(activity); 97 | setDispose(fragment1, disposable); 98 | } 99 | 100 | private static void setDispose(HolderLifeFragment fragment, Disposable disposable) { 101 | if (fragment != null) { 102 | fragment.setDispose(disposable); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /SimpleRxBus/src/main/java/com/example/simplerxbus/bus/RxSimpleBus.java: -------------------------------------------------------------------------------- 1 | package com.example.simplerxbus.bus; 2 | 3 | import java.util.concurrent.ConcurrentHashMap; 4 | import java.util.concurrent.ConcurrentMap; 5 | 6 | import io.reactivex.Observable; 7 | import io.reactivex.disposables.Disposable; 8 | import io.reactivex.functions.Function; 9 | import io.reactivex.functions.Predicate; 10 | import io.reactivex.subjects.PublishSubject; 11 | import io.reactivex.subjects.Subject; 12 | 13 | /** 14 | * Created by 拉丁吴 on 2018. 15 | */ 16 | 17 | class RxSimpleBus { 18 | 19 | private final Subject normalBus; 20 | private final Subject stickyBus; 21 | private final static RxSimpleBus INSTANCE = new RxSimpleBus(); 22 | private ConcurrentMap cache = new ConcurrentHashMap<>(); 23 | 24 | private RxSimpleBus() { 25 | normalBus = PublishSubject.create().toSerialized(); 26 | stickyBus = PublishSubject.create().toSerialized(); 27 | } 28 | 29 | public static RxSimpleBus getBus() { 30 | return INSTANCE; 31 | } 32 | 33 | public void sendMessage(RxBusMessage rxMsg) { 34 | normalBus.onNext(rxMsg); 35 | } 36 | 37 | public void sendStickyMessage(RxBusMessage rxMsg) { 38 | if (rxMsg == null || rxMsg.getMsg()==null ||rxMsg.getMsg()==null) { 39 | return; 40 | } 41 | cache.put(rxMsg.getType(),rxMsg); 42 | } 43 | 44 | private Observable toObserverable(Subject sub, final String filter) { 45 | return sub.map(new Function() { 46 | 47 | @Override 48 | public RxBusMessage apply(Object o) throws Exception { 49 | return (RxBusMessage) o; 50 | } 51 | }).filter(new Predicate() { 52 | @Override 53 | public boolean test(RxBusMessage rxBusMessage) throws Exception { 54 | return rxBusMessage.getType().equals(filter); 55 | } 56 | }).map(new Function() { 57 | 58 | @Override 59 | public Object apply(RxBusMessage rxBusMessage) throws Exception { 60 | return rxBusMessage.getMsg(); 61 | } 62 | }); 63 | } 64 | 65 | public Disposable receiveMessageFrom(final String filter, RxBusReceiver receiver) { 66 | return getBus().toObserverable(normalBus, filter).subscribeWith(receiver); 67 | } 68 | 69 | public Disposable receiveStickyMessage(final String filter, RxBusReceiver receiver) { 70 | Disposable disposable = getBus().toObserverable(stickyBus, filter).subscribeWith(receiver); 71 | Object value = cache.get(filter); 72 | if (value != null) { 73 | stickyBus.onNext(value); 74 | } 75 | return disposable; 76 | } 77 | 78 | private boolean hasObservable() { 79 | return normalBus.hasObservers(); 80 | } 81 | protected void clearCurMessage(String filter){ 82 | cache.remove(filter); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /SimpleRxBus/src/main/java/com/example/simplerxbus/lifecycle/EmptyActivityLifecycleCallbacks.java: -------------------------------------------------------------------------------- 1 | package com.example.simplerxbus.lifecycle; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.os.Bundle; 6 | 7 | public class EmptyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks { 8 | @Override 9 | public void onActivityCreated(Activity activity, Bundle savedInstanceState) { 10 | 11 | } 12 | 13 | @Override 14 | public void onActivityStarted(Activity activity) { 15 | 16 | } 17 | 18 | @Override 19 | public void onActivityResumed(Activity activity) { 20 | 21 | } 22 | 23 | @Override 24 | public void onActivityPaused(Activity activity) { 25 | 26 | } 27 | 28 | @Override 29 | public void onActivityStopped(Activity activity) { 30 | 31 | } 32 | 33 | @Override 34 | public void onActivitySaveInstanceState(Activity activity, Bundle outState) { 35 | 36 | } 37 | 38 | @Override 39 | public void onActivityDestroyed(Activity activity) { 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /SimpleRxBus/src/main/java/com/example/simplerxbus/lifecycle/HolderLifeFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.simplerxbus.lifecycle; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.annotation.RestrictTo; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v4.app.FragmentActivity; 10 | import android.support.v4.app.FragmentManager; 11 | import android.util.Log; 12 | 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | 16 | import io.reactivex.disposables.CompositeDisposable; 17 | import io.reactivex.disposables.Disposable; 18 | 19 | public class HolderLifeFragment extends Fragment { 20 | private static final String LOG_TAG = "ViewModelStores"; 21 | 22 | private static final HolderLifeFragment.HolderFragmentManager sHolderFragmentManager = new HolderLifeFragment.HolderFragmentManager(); 23 | private CompositeDisposable compositeDisposable = 24 | new CompositeDisposable(); 25 | 26 | 27 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 28 | public static final String HOLDER_TAG = 29 | "qsbk.app.utils.otto.HolderLifeFragment.tag"; 30 | 31 | 32 | public HolderLifeFragment() { 33 | setRetainInstance(true); 34 | } 35 | 36 | @Override 37 | public void onCreate(@Nullable Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | sHolderFragmentManager.holderFragmentCreated(this); 40 | } 41 | 42 | @Override 43 | public void onSaveInstanceState(Bundle outState) { 44 | super.onSaveInstanceState(outState); 45 | } 46 | 47 | @Override 48 | public void onDestroy() { 49 | super.onDestroy(); 50 | if (compositeDisposable != null) { 51 | compositeDisposable.clear(); 52 | } 53 | } 54 | 55 | public void setDispose(Disposable dispose) { 56 | if (compositeDisposable != null) { 57 | compositeDisposable.add(dispose); 58 | } 59 | } 60 | 61 | 62 | public static HolderLifeFragment holderFragmentFor(FragmentActivity activity) { 63 | if (activity == null) { 64 | return null; 65 | } 66 | return sHolderFragmentManager.holderFragmentFor(activity); 67 | } 68 | 69 | 70 | public static HolderLifeFragment holderFragmentFor(Fragment fragment) { 71 | if (fragment == null) { 72 | return null; 73 | } 74 | return sHolderFragmentManager.holderFragmentFor(fragment); 75 | } 76 | 77 | @SuppressWarnings("WeakerAccess") 78 | static class HolderFragmentManager { 79 | private Map mNotCommittedActivityHolders = new HashMap<>(); 80 | private Map mNotCommittedFragmentHolders = new HashMap<>(); 81 | 82 | private Application.ActivityLifecycleCallbacks mActivityCallbacks = 83 | new EmptyActivityLifecycleCallbacks() { 84 | @Override 85 | public void onActivityDestroyed(Activity activity) { 86 | HolderLifeFragment fragment = mNotCommittedActivityHolders.remove(activity); 87 | if (fragment != null) { 88 | Log.e(LOG_TAG, "Failed to save a ViewModel for " + activity); 89 | } 90 | } 91 | }; 92 | 93 | private boolean mActivityCallbacksIsAdded = false; 94 | 95 | private FragmentManager.FragmentLifecycleCallbacks mParentDestroyedCallback = 96 | new FragmentManager.FragmentLifecycleCallbacks() { 97 | @Override 98 | public void onFragmentDestroyed(FragmentManager fm, Fragment parentFragment) { 99 | super.onFragmentDestroyed(fm, parentFragment); 100 | HolderLifeFragment fragment = mNotCommittedFragmentHolders.remove( 101 | parentFragment); 102 | if (fragment != null) { 103 | Log.e(LOG_TAG, "Failed to save a ViewModel for " + parentFragment); 104 | } 105 | } 106 | }; 107 | 108 | void holderFragmentCreated(Fragment holderFragment) { 109 | Fragment parentFragment = holderFragment.getParentFragment(); 110 | if (parentFragment != null) { 111 | mNotCommittedFragmentHolders.remove(parentFragment); 112 | parentFragment.getFragmentManager().unregisterFragmentLifecycleCallbacks( 113 | mParentDestroyedCallback); 114 | } else { 115 | mNotCommittedActivityHolders.remove(holderFragment.getActivity()); 116 | } 117 | } 118 | 119 | private static HolderLifeFragment findHolderFragment(FragmentManager manager) { 120 | if (manager.isDestroyed()) { 121 | throw new IllegalStateException("Can't access ViewModels from onDestroy"); 122 | } 123 | 124 | Fragment fragmentByTag = manager.findFragmentByTag(HOLDER_TAG); 125 | if (fragmentByTag != null && !(fragmentByTag instanceof HolderLifeFragment)) { 126 | throw new IllegalStateException("Unexpected " 127 | + "fragment instance was returned by HOLDER_TAG"); 128 | } 129 | return (HolderLifeFragment) fragmentByTag; 130 | } 131 | 132 | private static HolderLifeFragment createHolderFragment(FragmentManager fragmentManager) { 133 | HolderLifeFragment holder = new HolderLifeFragment(); 134 | fragmentManager.beginTransaction().add(holder, HOLDER_TAG).commitAllowingStateLoss(); 135 | return holder; 136 | } 137 | 138 | HolderLifeFragment holderFragmentFor(FragmentActivity activity) { 139 | FragmentManager fm = activity.getSupportFragmentManager(); 140 | HolderLifeFragment holder = findHolderFragment(fm); 141 | if (holder != null) { 142 | return holder; 143 | } 144 | holder = mNotCommittedActivityHolders.get(activity); 145 | if (holder != null) { 146 | return holder; 147 | } 148 | 149 | if (!mActivityCallbacksIsAdded) { 150 | mActivityCallbacksIsAdded = true; 151 | activity.getApplication().registerActivityLifecycleCallbacks(mActivityCallbacks); 152 | } 153 | holder = createHolderFragment(fm); 154 | mNotCommittedActivityHolders.put(activity, holder); 155 | return holder; 156 | } 157 | 158 | HolderLifeFragment holderFragmentFor(Fragment parentFragment) { 159 | FragmentManager fm = parentFragment.getChildFragmentManager(); 160 | HolderLifeFragment holder = findHolderFragment(fm); 161 | if (holder != null) { 162 | return holder; 163 | } 164 | holder = mNotCommittedFragmentHolders.get(parentFragment); 165 | if (holder != null) { 166 | return holder; 167 | } 168 | 169 | parentFragment.getFragmentManager() 170 | .registerFragmentLifecycleCallbacks(mParentDestroyedCallback, false); 171 | holder = createHolderFragment(fm); 172 | mNotCommittedFragmentHolders.put(parentFragment, holder); 173 | return holder; 174 | } 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /SimpleRxBus/src/main/java/com/example/simplerxbus/observer/RxResourceObserver.java: -------------------------------------------------------------------------------- 1 | package com.example.simplerxbus.observer; 2 | 3 | import android.support.annotation.CallSuper; 4 | import android.support.annotation.NonNull; 5 | 6 | import java.util.concurrent.atomic.AtomicReference; 7 | 8 | import io.reactivex.Observer; 9 | import io.reactivex.disposables.Disposable; 10 | import io.reactivex.internal.disposables.DisposableHelper; 11 | import io.reactivex.internal.disposables.ListCompositeDisposable; 12 | import io.reactivex.internal.functions.ObjectHelper; 13 | import io.reactivex.plugins.RxJavaPlugins; 14 | 15 | 16 | public abstract class RxResourceObserver implements Observer, Disposable { 17 | /** The active subscription. */ 18 | private final AtomicReference s = new AtomicReference(); 19 | 20 | /** The resource composite, can never be null. */ 21 | private final ListCompositeDisposable resources = new ListCompositeDisposable(); 22 | 23 | /** 24 | * Adds a resource to this ResourceObserver. 25 | * 26 | * @param resource the resource to add 27 | * 28 | * @throws NullPointerException if resource is null 29 | */ 30 | public final void add(@NonNull Disposable resource) { 31 | ObjectHelper.requireNonNull(resource, "resource is null"); 32 | resources.add(resource); 33 | } 34 | 35 | @Override 36 | public final void onSubscribe(Disposable s) { 37 | if (setOnce(this.s, s, getClass())) { 38 | onStart(); 39 | } 40 | } 41 | 42 | private boolean setOnce(AtomicReference upstream, Disposable next, Class observer) { 43 | ObjectHelper.requireNonNull(next, "next is null"); 44 | if (!upstream.compareAndSet(null, next)) { 45 | next.dispose(); 46 | if (upstream.get() != DisposableHelper.DISPOSED) { 47 | reportDoubleSubscription(observer); 48 | } 49 | return false; 50 | } 51 | return true; 52 | } 53 | 54 | private void reportDoubleSubscription(Class consumer) { 55 | RxJavaPlugins.onError(new IllegalStateException(composeMessage(consumer.getName()))); 56 | } 57 | 58 | public String composeMessage(String consumer) { 59 | return "It is not allowed to subscribe with a(n) " + consumer + " multiple times. " 60 | + "Please create a fresh instance of " + consumer + " and subscribe that to the target source instead."; 61 | } 62 | /** 63 | * Called once the upstream sets a Subscription on this ResourceObserver. 64 | * 65 | *

You can perform initialization at this moment. The default 66 | * implementation does nothing. 67 | */ 68 | protected void onStart() { 69 | } 70 | 71 | /** 72 | * Cancels the main disposable (if any) and disposes the resources associated with 73 | * this ResourceObserver (if any). 74 | * 75 | *

This method can be called before the upstream calls onSubscribe at which 76 | * case the main Disposable will be immediately disposed. 77 | */ 78 | @CallSuper 79 | @Override 80 | public void dispose() { 81 | if (DisposableHelper.dispose(s)) { 82 | resources.dispose(); 83 | } 84 | } 85 | 86 | /** 87 | * Returns true if this ResourceObserver has been disposed/cancelled. 88 | * @return true if this ResourceObserver has been disposed/cancelled 89 | */ 90 | @Override 91 | public final boolean isDisposed() { 92 | return DisposableHelper.isDisposed(s.get()); 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /SimpleRxBus/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | My Library 3 | 4 | -------------------------------------------------------------------------------- /SimpleRxBus/src/test/java/com/example/simplerxbus/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.example.simplerxbus; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | //apply plugin: 'kotlin-android' 4 | 5 | //apply plugin: 'kotlin-android-extensions' 6 | // 7 | android { 8 | compileSdkVersion 28 9 | defaultConfig { 10 | applicationId "com.example.laddingwu.rxbusapplication" 11 | minSdkVersion 14 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation 'com.android.support:appcompat-v7:28.0.0' 28 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 29 | implementation 'com.android.support:support-v4:28.0.0' 30 | implementation 'com.google.android.gms:play-services-plus:16.0.0' 31 | testImplementation 'junit:junit:4.12' 32 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 33 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 34 | implementation project(':SimpleRxBus') 35 | implementation "io.reactivex.rxjava2:rxjava:2.1.8" 36 | implementation "io.reactivex.rxjava2:rxandroid:2.0.1" 37 | // implementation 'com.ladingwu.library:SimpleRxBus:0.1' 38 | } 39 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/laddingwu/rxbusapplication/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.example.laddingwu.rxbusapplication 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("com.example.laddingwu.rxbusapplication", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laddingwu/rxbusapplication/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.laddingwu.rxbusapplication; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.View; 8 | import android.widget.EditText; 9 | import android.widget.TextView; 10 | 11 | import com.example.laddingwu.rxbusapplication.model.ActivityToFragmentsBean; 12 | import com.example.simplerxbus.bus.RxBusReceiver; 13 | import com.example.simplerxbus.bus.RxBusUtils; 14 | 15 | public class MainActivity extends AppCompatActivity{ 16 | EditText editText; 17 | TextView textView; 18 | @Override 19 | protected void onCreate(@Nullable Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_main); 22 | editText = findViewById(R.id.input); 23 | textView = findViewById(R.id.result); 24 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 25 | @Override 26 | public void onClick(View view) { 27 | ActivityToFragmentsBean bean = new ActivityToFragmentsBean(); 28 | bean.data1 = "data1 from MainActivity"; 29 | bean.data2 = "data2 also from MainActivity"; 30 | RxBusUtils.postSticky(Tag.BUS_CLICK_FRIST, bean); 31 | startActivity(new Intent(MainActivity.this,SecondActivity.class)); 32 | } 33 | }); 34 | RxBusUtils.receive(this,Tag.BUS_CLICK_FRIST_RESULT, new RxBusReceiver() { 35 | @Override 36 | public void receive(Object data) { 37 | if (data instanceof String) { 38 | textView.setText((String) data); 39 | 40 | } 41 | } 42 | }); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laddingwu/rxbusapplication/SecondActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.laddingwu.rxbusapplication; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | import android.widget.TextView; 7 | 8 | import com.example.laddingwu.rxbusapplication.fragments.FristFragment; 9 | import com.example.laddingwu.rxbusapplication.fragments.SecondFragment; 10 | import com.example.simplerxbus.bus.RxBusUtils; 11 | 12 | public class SecondActivity extends AppCompatActivity { 13 | 14 | TextView tv; 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_second); 19 | 20 | findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { 21 | @Override 22 | public void onClick(View view) { 23 | RxBusUtils.post(Tag.BUS_CLICK_FRIST_RESULT, "data from secondActivity"); 24 | finish(); 25 | } 26 | }); 27 | getSupportFragmentManager().beginTransaction().replace(R.id.framelayout_1, new FristFragment()).commitAllowingStateLoss(); 28 | getSupportFragmentManager().beginTransaction().replace(R.id.framelayout_2, new SecondFragment()).commitAllowingStateLoss(); 29 | }; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laddingwu/rxbusapplication/Tag.java: -------------------------------------------------------------------------------- 1 | package com.example.laddingwu.rxbusapplication; 2 | 3 | public class Tag { 4 | public static String BUS_CLICK_FRIST = "bus_click_frist"; 5 | public static String BUS_CLICK_FRIST_RESULT = "bus_click_frist_result"; 6 | } 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laddingwu/rxbusapplication/ThridActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.laddingwu.rxbusapplication; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | public class ThridActivity extends AppCompatActivity { 7 | 8 | @Override 9 | protected void onCreate(Bundle savedInstanceState) { 10 | super.onCreate(savedInstanceState); 11 | setContentView(R.layout.activity_thrid); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laddingwu/rxbusapplication/fragments/FristFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laddingwu.rxbusapplication.fragments; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.support.v4.app.Fragment; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.TextView; 11 | 12 | import com.example.laddingwu.rxbusapplication.R; 13 | import com.example.laddingwu.rxbusapplication.Tag; 14 | import com.example.laddingwu.rxbusapplication.model.ActivityToFragmentsBean; 15 | import com.example.simplerxbus.bus.RxBusReceiver; 16 | import com.example.simplerxbus.bus.RxBusUtils; 17 | 18 | /** 19 | * A fragment with a Google +1 button. 20 | * Use the {@link FristFragment#newInstance} factory method to 21 | * create an instance of this fragment. 22 | */ 23 | public class FristFragment extends Fragment { 24 | // TODO: Rename parameter arguments, choose names that match 25 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 26 | private static final String ARG_PARAM1 = "param1"; 27 | private static final String ARG_PARAM2 = "param2"; 28 | // The request code must be 0 or greater. 29 | private static final int PLUS_ONE_REQUEST_CODE = 0; 30 | // The URL to +1. Must be a valid URL. 31 | private final String PLUS_ONE_URL = "http://developer.android.com"; 32 | // TODO: Rename and change types of parameters 33 | private String mParam1; 34 | private String mParam2; 35 | 36 | 37 | public FristFragment() { 38 | // Required empty public constructor 39 | } 40 | 41 | /** 42 | * Use this factory method to create a new instance of 43 | * this fragment using the provided parameters. 44 | * 45 | * @param param1 Parameter 1. 46 | * @param param2 Parameter 2. 47 | * @return A new instance of fragment FristFragment. 48 | */ 49 | // TODO: Rename and change types and number of parameters 50 | public static FristFragment newInstance(String param1, String param2) { 51 | FristFragment fragment = new FristFragment(); 52 | Bundle args = new Bundle(); 53 | args.putString(ARG_PARAM1, param1); 54 | args.putString(ARG_PARAM2, param2); 55 | fragment.setArguments(args); 56 | return fragment; 57 | } 58 | 59 | @Override 60 | public void onCreate(Bundle savedInstanceState) { 61 | super.onCreate(savedInstanceState); 62 | if (getArguments() != null) { 63 | mParam1 = getArguments().getString(ARG_PARAM1); 64 | mParam2 = getArguments().getString(ARG_PARAM2); 65 | } 66 | } 67 | 68 | private TextView textView; 69 | 70 | @Override 71 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 72 | Bundle savedInstanceState) { 73 | // Inflate the layout for this fragment 74 | View view = inflater.inflate(R.layout.fragment_plus_one, container, false); 75 | textView = view.findViewById(R.id.text_id); 76 | TextView tv = view.findViewById(R.id.tv1); 77 | tv.setText("FristFragment getData: "); 78 | //Find the +1 button 79 | 80 | return view; 81 | } 82 | 83 | @Override 84 | public void onResume() { 85 | super.onResume(); 86 | RxBusUtils.receiveSticky(this, Tag.BUS_CLICK_FRIST, new RxBusReceiver() { 87 | @Override 88 | public void receive(Object data) { 89 | if (data instanceof ActivityToFragmentsBean) { 90 | Log.w("fragment===> ", ((ActivityToFragmentsBean) data).data1); 91 | textView.setText(((ActivityToFragmentsBean) data).data1); 92 | } 93 | } 94 | }); 95 | // Refresh the state of the +1 button each time the activity receives focus. 96 | } 97 | 98 | 99 | } 100 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laddingwu/rxbusapplication/fragments/SecondFragment.java: -------------------------------------------------------------------------------- 1 | package com.example.laddingwu.rxbusapplication.fragments; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.support.v4.app.Fragment; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.TextView; 11 | 12 | import com.example.laddingwu.rxbusapplication.R; 13 | import com.example.laddingwu.rxbusapplication.Tag; 14 | import com.example.laddingwu.rxbusapplication.model.ActivityToFragmentsBean; 15 | import com.example.simplerxbus.bus.RxBusReceiver; 16 | import com.example.simplerxbus.bus.RxBusUtils; 17 | 18 | /** 19 | * A fragment with a Google +1 button. 20 | * Use the {@link SecondFragment#newInstance} factory method to 21 | * create an instance of this fragment. 22 | */ 23 | public class SecondFragment extends Fragment { 24 | // TODO: Rename parameter arguments, choose names that match 25 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 26 | private static final String ARG_PARAM1 = "param1"; 27 | private static final String ARG_PARAM2 = "param2"; 28 | // The request code must be 0 or greater. 29 | private static final int PLUS_ONE_REQUEST_CODE = 0; 30 | // The URL to +1. Must be a valid URL. 31 | private final String PLUS_ONE_URL = "http://developer.android.com"; 32 | // TODO: Rename and change types of parameters 33 | private String mParam1; 34 | private String mParam2; 35 | 36 | 37 | public SecondFragment() { 38 | // Required empty public constructor 39 | } 40 | 41 | /** 42 | * Use this factory method to create a new instance of 43 | * this fragment using the provided parameters. 44 | * 45 | * @param param1 Parameter 1. 46 | * @param param2 Parameter 2. 47 | * @return A new instance of fragment FristFragment. 48 | */ 49 | // TODO: Rename and change types and number of parameters 50 | public static SecondFragment newInstance(String param1, String param2) { 51 | SecondFragment fragment = new SecondFragment(); 52 | Bundle args = new Bundle(); 53 | args.putString(ARG_PARAM1, param1); 54 | args.putString(ARG_PARAM2, param2); 55 | fragment.setArguments(args); 56 | return fragment; 57 | } 58 | 59 | @Override 60 | public void onCreate(Bundle savedInstanceState) { 61 | super.onCreate(savedInstanceState); 62 | if (getArguments() != null) { 63 | mParam1 = getArguments().getString(ARG_PARAM1); 64 | mParam2 = getArguments().getString(ARG_PARAM2); 65 | } 66 | } 67 | 68 | private TextView textView; 69 | 70 | @Override 71 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 72 | Bundle savedInstanceState) { 73 | // Inflate the layout for this fragment 74 | View view = inflater.inflate(R.layout.fragment_plus_one, container, false); 75 | textView = view.findViewById(R.id.text_id); 76 | TextView tv = view.findViewById(R.id.tv1); 77 | tv.setText("SecondFragment getData: "); 78 | 79 | //Find the +1 button 80 | 81 | return view; 82 | } 83 | 84 | @Override 85 | public void onResume() { 86 | super.onResume(); 87 | RxBusUtils.receiveSticky(this, Tag.BUS_CLICK_FRIST, new RxBusReceiver() { 88 | @Override 89 | public void receive(Object data) { 90 | if (data instanceof ActivityToFragmentsBean) { 91 | Log.w("fragment===> ", ((ActivityToFragmentsBean) data).data2); 92 | textView.setText(((ActivityToFragmentsBean) data).data2); 93 | } 94 | } 95 | }); 96 | // Refresh the state of the +1 button each time the activity receives focus. 97 | } 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/laddingwu/rxbusapplication/model/ActivityToFragmentsBean.java: -------------------------------------------------------------------------------- 1 | package com.example.laddingwu.rxbusapplication.model; 2 | 3 | public class ActivityToFragmentsBean { 4 | public String data1; 5 | public String data2; 6 | } 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 11 | 16 | 21 | 26 | 31 | 36 | 41 | 46 | 51 | 56 | 61 | 66 | 71 | 76 | 81 | 86 | 91 | 96 | 101 | 106 | 111 | 116 | 121 | 126 | 131 | 136 | 141 | 146 | 151 | 156 | 161 | 166 | 171 | 172 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 20 | 21 |