Advertisement
Guest User

rxjava leak 2

a guest
May 30th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.lacronicus.rxjavaleaktest.app;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.widget.Toast;
  8.  
  9. import rx.Observable;
  10. import rx.Subscription;
  11. import rx.android.schedulers.AndroidSchedulers;
  12. import rx.functions.Action0;
  13. import rx.functions.Action1;
  14. import rx.functions.Func1;
  15. import rx.schedulers.Schedulers;
  16.  
  17.  
  18. public class MainActivity extends Activity {
  19. Subscription s;
  20.  
  21. //this represents a long-running background action. shouldn't have a reference to the activity
  22. private static class myMapFunction implements Func1<Integer, Integer> {
  23. @Override
  24. public Integer call(Integer integer) {
  25. int x = 1;
  26. while (x == 1) {
  27. try {
  28. Thread.sleep(10000);
  29. } catch (InterruptedException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. return 1;
  34. }
  35. }
  36.  
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42. s = Observable.from(1).observeOn(Schedulers.io()).map(new myMapFunction()).observeOn(AndroidSchedulers.mainThread()).subscribe(
  43. new Action1<Integer>() {
  44. @Override
  45. public void call(Integer integer) {
  46. Toast.makeText(MainActivity.this, "lol", Toast.LENGTH_SHORT).show();
  47. }
  48. }, new Action1<Throwable>() {
  49. @Override
  50. public void call(Throwable throwable) {
  51. Toast.makeText(MainActivity.this, "lol", Toast.LENGTH_SHORT).show();
  52. }
  53. }, new Action0() {
  54. @Override
  55. public void call() {
  56. Toast.makeText(MainActivity.this, "lol", Toast.LENGTH_SHORT).show();
  57. }
  58. }
  59. );
  60. }
  61.  
  62. @Override
  63. protected void onStop() {
  64. super.onStop();
  65. if (s != null)
  66. s.unsubscribe();
  67. }
  68.  
  69. @Override
  70. public boolean onCreateOptionsMenu(Menu menu) {
  71. // Inflate the menu; this adds items to the action bar if it is present.
  72. getMenuInflater().inflate(R.menu.main, menu);
  73. return true;
  74. }
  75.  
  76. @Override
  77. public boolean onOptionsItemSelected(MenuItem item) {
  78. // Handle action bar item clicks here. The action bar will
  79. // automatically handle clicks on the Home/Up button, so long
  80. // as you specify a parent activity in AndroidManifest.xml.
  81. int id = item.getItemId();
  82. if (id == R.id.action_settings) {
  83. return true;
  84. }
  85. return super.onOptionsItemSelected(item);
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement