|
| 1 | +package com.rxjava2.android.samples.ui.rxbus; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.v7.app.AppCompatActivity; |
| 5 | +import android.view.View; |
| 6 | +import android.widget.Button; |
| 7 | +import android.widget.TextView; |
| 8 | + |
| 9 | +import com.rxjava2.android.samples.MyApplication; |
| 10 | +import com.rxjava2.android.samples.R; |
| 11 | +import com.rxjava2.android.samples.model.Events; |
| 12 | + |
| 13 | +import io.reactivex.android.schedulers.AndroidSchedulers; |
| 14 | +import io.reactivex.disposables.CompositeDisposable; |
| 15 | +import io.reactivex.functions.Consumer; |
| 16 | +import io.reactivex.schedulers.Schedulers; |
| 17 | + |
| 18 | +/** |
| 19 | + * Created by amitshekhar on 06/02/17. |
| 20 | + */ |
| 21 | + |
| 22 | +public class RxBusActivity extends AppCompatActivity { |
| 23 | + |
| 24 | + public static final String TAG = RxBusActivity.class.getSimpleName(); |
| 25 | + TextView textView; |
| 26 | + Button button; |
| 27 | + private final CompositeDisposable disposables = new CompositeDisposable(); |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void onDestroy() { |
| 31 | + super.onDestroy(); |
| 32 | + disposables.clear(); // do not send event after activity has been destroyed |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected void onCreate(Bundle savedInstanceState) { |
| 37 | + super.onCreate(savedInstanceState); |
| 38 | + setContentView(R.layout.activity_rxbus); |
| 39 | + textView = (TextView) findViewById(R.id.textView); |
| 40 | + button = (Button) findViewById(R.id.button); |
| 41 | + |
| 42 | + disposables.add(((MyApplication) getApplication()) |
| 43 | + .bus() |
| 44 | + .toObserverable() |
| 45 | + .subscribeOn(Schedulers.io()) |
| 46 | + .observeOn(AndroidSchedulers.mainThread()) |
| 47 | + .subscribe(new Consumer<Object>() { |
| 48 | + @Override |
| 49 | + public void accept(Object object) throws Exception { |
| 50 | + if (object instanceof Events.AutoEvent) { |
| 51 | + textView.setText("Auto Event Received"); |
| 52 | + } else if (object instanceof Events.TapEvent) { |
| 53 | + textView.setText("Tap Event Received"); |
| 54 | + } |
| 55 | + } |
| 56 | + })); |
| 57 | + |
| 58 | + button.setOnClickListener(new View.OnClickListener() { |
| 59 | + @Override |
| 60 | + public void onClick(View v) { |
| 61 | + ((MyApplication) getApplication()) |
| 62 | + .bus() |
| 63 | + .send(new Events.TapEvent()); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +} |
0 commit comments