View difference between Paste ID: VUZ5aApe and e9zPr5Ki
SHOW: | | - or go back to the newest paste.
1
package com.lacronicus.rxjavaleaktest.app;
2
3
import android.app.Activity;
4
import android.os.Bundle;
5-
import android.view.Menu;
5+
6-
import android.view.MenuItem;
6+
7
import rx.Observable;
8
import rx.Subscription;
9
import rx.android.schedulers.AndroidSchedulers;
10
import rx.functions.Action1;
11
import rx.functions.Func1;
12
import rx.schedulers.Schedulers;
13
14
15
public class MainActivity extends Activity {
16
    Subscription s;
17
18
    //this represents a long-running background action. shouldn't have a reference to the activity
19
    private static class myMapFunction implements Func1<Integer, Integer> {
20
        @Override
21
        public Integer call(Integer integer) {
22
            int x = 1;
23
            while (x == 1) {
24
                try {
25
                    Thread.sleep(10000);
26
                } catch (InterruptedException e) {
27
                    e.printStackTrace();
28
                }
29
            }
30
            return 1;
31
        }
32
    }
33
34
35
    @Override
36
    protected void onCreate(Bundle savedInstanceState) {
37
        super.onCreate(savedInstanceState);
38
        setContentView(R.layout.activity_main);
39
        s = Observable.from(1).observeOn(Schedulers.io()).map(new myMapFunction()).observeOn(AndroidSchedulers.mainThread()).subscribe(
40
                new Action1<Integer>() {
41
                    @Override
42
                    public void call(Integer integer) {
43
                        Toast.makeText(MainActivity.this, "lol", Toast.LENGTH_SHORT).show();
44
                    }
45
                }
46
        );
47
    }
48
49
    @Override
50
    protected void onStop() {
51
        super.onStop();
52
        if (s != null)
53
            s.unsubscribe();
54
    }
55
}