title: Android - LiveData is not unsubscribed when fragment is refreshed
body: <p>So I have a weird scenario. I implemented an app which has a single Activity and two fragments. Both fragments have a filter option where they can select a category. </p>
  
  <p>Based on the category, the current fragment will refresh &amp; fetch data with the new category. </p>
  
  <p>I am using ViewModel to fetch the data and LiveData to observe the changes.</p>
  
  <p>My <a href="https://github.com/anitaa1990/TrailersApp/blob/master/app/src/main/java/com/an/trailers/ui/main/fragment/MovieListFragment.java" rel="nofollow noreferrer">Fragment</a> class and My <a href="https://github.com/anitaa1990/TrailersApp/blob/master/app/src/main/java/com/an/trailers/ui/main/viewmodel/MovieListViewModel.java" rel="nofollow noreferrer">ViewModel</a> class</p>
  
  <pre><code>When a button is clicked in the Activity class, this method is called:
  public static void replaceFragment(Activity activity,
                                         int navId,
                                         int selectedPosition) {
          Bundle bundle = new Bundle();
          bundle.putInt(INTENT_CATEGORY, selectedPosition);
          Navigation.findNavController(activity, R.id.fragment_nav_host)
                  .navigate(navId, bundle, new NavOptions.Builder()
                          .setEnterAnim(R.anim.flip_right_in)
                          .setExitAnim(R.anim.flip_right_out)
                          .setPopEnterAnim(R.anim.flip_left_in)
                          .setPopExitAnim(R.anim.flip_left_out)
                          .build());
      }
  </code></pre>
  
  <p>I am using Navigation component. So the weird issue is that when I am refreshing the fragment, the LiveData is called multiple times. I understand that when the fragment is recreated or refreshed, the viewModel instance is still the same. But not sure why this issue is happening.</p>
  
  <p>If anyone can help clear this issue, would be grateful. </p>
  
  <p>The entire app code can be found <a href="https://github.com/anitaa1990/TrailersApp" rel="nofollow noreferrer">here</a></p>
  
  <p><strong>UPDATE:</strong>
  So if I change my initialiseViewModel() method from:</p>
  
  <pre><code>private void initialiseViewModel() {
      moviesListViewModel = ViewModelProviders.of(this, viewModelFactory).get(MovieListViewModel.class);
      moviesListViewModel.fetchMovies(MENU_MOVIE_ITEM.get(getArguments() == null ? 0: getArguments().getInt(INTENT_CATEGORY)));
      moviesListViewModel.getMoviesLiveData().observe(this, resource -&gt; {
          if(resource.isLoading()) {
              displayLoader();
  
          } else if(resource.data != null &amp;&amp; !resource.data.isEmpty()) {
              handleSuccessResponse(resource.data);
  
          } else handleErrorResponse();
      });
  }
  </code></pre>
  
  <p>TO</p>
  
  <pre><code>    private void initialiseViewModel() {
  
          AppDatabase appDatabase = Room.databaseBuilder(getContext(),
                  AppDatabase.class, "Entertainment.db")
                  .allowMainThreadQueries().build();
  
          HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
          logging.setLevel(HttpLoggingInterceptor.Level.BODY);
  
          OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
          httpClient.addInterceptor(logging);
          httpClient.addNetworkInterceptor(new RequestInterceptor());
          httpClient.connectTimeout(30, TimeUnit.SECONDS);
          httpClient.readTimeout(30, TimeUnit.SECONDS);
  
          MovieApiService movieApiService =
                  new Retrofit.Builder()
                          .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().create()))
                          .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                          .baseUrl(BASE_URL)
                          .client(httpClient.build())
                          .build().create(MovieApiService.class);
  
  //        moviesListViewModel = ViewModelProviders.of(this, viewModelFactory).get(MovieListViewModel.class);
          moviesListViewModel = new MovieListViewModel(appDatabase.movieDao(), movieApiService);
          moviesListViewModel.fetchMovies(MENU_MOVIE_ITEM.get(getArguments() == null ? 0: getArguments().getInt(INTENT_CATEGORY)));
          moviesListViewModel.getMoviesLiveData().observe(this, resource -&gt; {
              if(resource.isLoading()) {
                  displayLoader();
  
              } else if(resource.data != null &amp;&amp; !resource.data.isEmpty()) {
                  handleSuccessResponse(resource.data);
  
              } else handleErrorResponse();
          });
      }
  </code></pre>
  
  <p>then the issue seems to go away :(. Any pointers as to why?</p>