Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.lacronicus.rxjavaleaktest.app;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Toast;
- import rx.Observable;
- import rx.Subscription;
- import rx.android.schedulers.AndroidSchedulers;
- import rx.functions.Action1;
- import rx.functions.Func1;
- import rx.schedulers.Schedulers;
- public class MainActivity extends Activity {
- Subscription s;
- //this represents a long-running background action. shouldn't have a reference to the activity
- private static class myMapFunction implements Func1<Integer, Integer> {
- @Override
- public Integer call(Integer integer) {
- int x = 1;
- while (x == 1) {
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- return 1;
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- s = Observable.from(1).observeOn(Schedulers.io()).map(new myMapFunction()).observeOn(AndroidSchedulers.mainThread()).subscribe(
- new Action1<Integer>() {
- @Override
- public void call(Integer integer) {
- Toast.makeText(MainActivity.this, "lol", Toast.LENGTH_SHORT).show();
- }
- }
- );
- }
- @Override
- protected void onStop() {
- super.onStop();
- if (s != null)
- s.unsubscribe();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement