GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: cleanup · linuxgg/RxJava-Android-Samples@1770d85

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • app/src/main/java/com/morihacky/android/rxjava
    • MainActivity.java
    • RotationPersistFragment.java
    • RotationPersistWorkerFragment.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+10Lines changed: 10 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ Another variant of the exponential backoff strategy is to execute an operation f
131131

132132
Simulating this behavior is actually way more simpler than the prevoius retry mechanism. You can use a variant of the `delay` operator to achieve this.
133133

134+
### Rotation Persist
135+
136+
A common question that's asked when using RxJava in Android is, "how do i resume the work of an observable if a configuration change occurs (activity rotation, language locale change etc.)?".
137+
138+
This example shows you one strategy viz. using retained Fragments. I started using retained fragments as "worker fragments" after reading this [fantastic post by Alex Lockwood](http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html) quite sometime back.
139+
140+
Hit the start button and rotate the screen to your heart's content; you'll see the observable continue from where it left out.
141+
142+
*There are certain quirks about the source "hot" observable used in this example. Watch out for a blog post where i explain the specifics. I would NOT suggest using this strategy for intentionally making your observables hot.*
143+
134144
## Work in Progress:
135145

136146
Examples that I would like to have here, but haven't found the time yet to flush out.
Collapse file

‎app/src/main/java/com/morihacky/android/rxjava/MainActivity.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/MainActivity.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
2424

2525
if (savedInstanceState == null) {
2626
getSupportFragmentManager().beginTransaction()
27-
//.replace(android.R.id.content, new MainFragment(), this.toString())
28-
.replace(android.R.id.content, new RotationPersistFragment(), this.toString())
27+
.replace(android.R.id.content, new MainFragment(), this.toString())
2928
.commit();
3029
}
3130
}
Collapse file

‎app/src/main/java/com/morihacky/android/rxjava/RotationPersistFragment.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/RotationPersistFragment.java
+27-29Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public class RotationPersistFragment
2626
extends BaseFragment
2727
implements RotationPersistWorkerFragment.IAmYourMaster {
2828

29+
public static final String FRAG_TAG = RotationPersistWorkerFragment.class.getName();
30+
2931
@InjectView(R.id.list_threading_log) ListView _logList;
32+
3033
private LogAdapter _adapter;
3134
private List<String> _logs;
3235

@@ -56,7 +59,6 @@ public View onCreateView(LayoutInflater inflater,
5659
@Override
5760
public void onPause() {
5861
super.onPause();
59-
6062
RxUtils.unsubscribeIfNotNull(_subscriptions);
6163
}
6264

@@ -67,48 +69,44 @@ public void startRetryingWithExponentialBackoffStrategy() {
6769
_logs = new ArrayList<>();
6870
_adapter.clear();
6971

70-
String FRAG_TAG = RotationPersistWorkerFragment.class.getName();
7172
FragmentManager fm = getActivity().getSupportFragmentManager();
7273
RotationPersistWorkerFragment frag =//
7374
(RotationPersistWorkerFragment) fm.findFragmentByTag(FRAG_TAG);
7475

7576
if (frag == null) {
76-
Timber.d("---- never created frag before");
77-
78-
// we've never created the frag before
7977
frag = new RotationPersistWorkerFragment();
8078
fm.beginTransaction().add(frag, FRAG_TAG).commit();
8179
} else {
82-
Timber.d("---- Worker frag already spawned");
80+
Timber.d("Worker frag already spawned");
8381
}
8482
}
8583

8684
@Override
8785
public void observeResults(Observable<Integer> ints) {
88-
Timber.d("---- observable instance ints %s", ints.toString());
89-
90-
_subscriptions.add(ints.doOnSubscribe(new Action0() {
91-
@Override
92-
public void call() {
93-
_log("Subscribing to ints");
94-
}
95-
}).subscribe(new Observer<Integer>() {
96-
@Override
97-
public void onCompleted() {
98-
_log("Observable is complete");
99-
}
10086

101-
@Override
102-
public void onError(Throwable e) {
103-
Timber.e(e, "Error in worker demo frag observable");
104-
_log("Dang! something went wrong.");
105-
}
106-
107-
@Override
108-
public void onNext(Integer integer) {
109-
_log(String.format("Worker frag spits out - %d", integer));
110-
}
111-
}));
87+
_subscriptions.add(//
88+
ints.doOnSubscribe(new Action0() {
89+
@Override
90+
public void call() {
91+
_log("Subscribing to ints");
92+
}
93+
}).subscribe(new Observer<Integer>() {
94+
@Override
95+
public void onCompleted() {
96+
_log("Observable is complete");
97+
}
98+
99+
@Override
100+
public void onError(Throwable e) {
101+
Timber.e(e, "Error in worker demo frag observable");
102+
_log("Dang! something went wrong.");
103+
}
104+
105+
@Override
106+
public void onNext(Integer integer) {
107+
_log(String.format("Worker frag spits out - %d", integer));
108+
}
109+
}));
112110
}
113111

114112
// -----------------------------------------------------------------------------------
Collapse file

‎app/src/main/java/com/morihacky/android/rxjava/RotationPersistWorkerFragment.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/RotationPersistWorkerFragment.java
+8-15Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.concurrent.TimeUnit;
88
import rx.Observable;
99
import rx.functions.Func1;
10-
import timber.log.Timber;
1110

1211
public class RotationPersistWorkerFragment
1312
extends Fragment {
@@ -16,35 +15,28 @@ public class RotationPersistWorkerFragment
1615
private IAmYourMaster _masterFrag;
1716

1817
/**
19-
* hold a reference to the activity -> caller fragment
18+
* Hold a reference to the activity -> caller fragment
2019
* this way when the worker frag kicks off
2120
* we can talk back to the master and send results
2221
*/
2322
@Override
2423
public void onAttach(Activity activity) {
2524
super.onAttach(activity);
2625

27-
_masterFrag = (RotationPersistFragment) getActivity().getSupportFragmentManager()
28-
.findFragmentByTag(RotationPersistFragment.class.getName());
29-
3026
List<Fragment> frags = ((MainActivity) activity).getSupportFragmentManager().getFragments();
31-
32-
Timber.d("---- there are currently %d fragments attached", frags.size());
33-
3427
for (Fragment f : frags) {
3528
if (f instanceof IAmYourMaster) {
3629
_masterFrag = (IAmYourMaster) f;
3730
}
3831
}
3932

4033
if (_masterFrag == null) {
41-
throw new ClassCastException("We did not find a master who can understand us :( !");
34+
throw new ClassCastException("We did not find a master who can understand us :(");
4235
}
4336
}
4437

4538
/**
46-
* This method will only be called once when the retained
47-
* Fragment is first created.
39+
* This method will only be called once when the retained Fragment is first created.
4840
*/
4941
@Override
5042
public void onCreate(Bundle savedInstanceState) {
@@ -57,8 +49,6 @@ public void onCreate(Bundle savedInstanceState) {
5749
return;
5850
}
5951

60-
// Create and execute the "Hot" observable
61-
Timber.d("---- Creating a new ints observable");
6252
_intsObservable =//
6353
Observable.interval(1, TimeUnit.SECONDS)//
6454
.map(new Func1<Long, Integer>() {
@@ -67,13 +57,16 @@ public Integer call(Long aLong) {
6757
return aLong.intValue();
6858
}
6959
})//
70-
.take(20);// cause i don't want this thing to run indefinitely
60+
.take(20);
7161

7262
// -----------------------------------------------------------------------------------
7363
// Making our observable "HOT" for the purpose of the demo.
7464

7565
_intsObservable = _intsObservable.share();
76-
// Do not do this in production! (i blame Jake Wharton for giving me this clever idea)
66+
67+
// Do not do this in production!
68+
// `.share` is "warm" not "hot"
69+
// the below forceful subscription fakes the heat
7770
_intsObservable.subscribe();
7871
}
7972

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.