title: android best practice to put time-consumed logic in fragment
body: <p>I'm building android app with multiple fragments on one activity and looking for best practice to load content.<br />
  my structure is like this</p>
  <pre><code>Activity
  ---- home fragment
  ---- second fragment
  ---- third fragment
  ..
  ---- another fragments
  </code></pre>
  <p>where should i put logic that cost time to load data. i want to show default content of fragment with progress dialog or some loading things so user know the app is on preparing data.</p>
  <p>looking at fragment lifecycle, i tried put onResume but my fragment not changing until the data is loaded (no default content show up)</p>
  <p>UPDATE 1:<br />
  I have implement LiveData and have small issue.</p>
  <pre><code>model = new ViewModelProvider(this).get(MainViewModel.class);
  model.getResult().observe(this, result -&gt; {
      Thread.sleep(2000);
      loading.dismiss();
  });
  
  model.setValue(&quot;ok&quot;);
  </code></pre>
  <p>this code on my activity. if i put sleep inside the observe, my page will show up with loading view and disappear after 2 seconds. but if i put sleep inside the MainViewModel method like this one</p>
  <pre><code>public void setValue(String value){
      Thread.sleep(2000);
      result.postValue(value);
  }
  </code></pre>
  <p>my app will wait 2 seconds and then new page will show up without loading view. Is this how LiveData works or i just miss something?</p>
  <p>UPDATE 2:<br />
  i have implement with real code. i still get issue my page (mostly) not showing with loading view.</p>
  <p>i have try put my load data code in activity and in modelView and both give result same, sometimes it show loading view then load data, sometimes it freeze and suddenly new page show up with the data already loaded</p>
  <p>UPDATE 3:<br />
  not proper one but currently i put delay before it start load the data. here is the code<br />
  <strong>DetailActivity</strong></p>
  <pre><code>public class DetailActivity extends AppCompatActivity {
    Data data
  
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // start show loading view
      loading.show();
      
      // init ViewModel
      setHomeViewModel();
      
      // set fragment
      changePage(Config.PAGE_DETAIL_HOME);
      
      // i put delay bit here just to wait loading view to show up
      new Handler(Looper.getMainLooper()).postDelayed(this::startCompile, 200);
      
      // previously i start compile data by just call the method
      // startCompile();
    }
  
    private void setHomeViewModel() {
      homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
  
      homeViewModel.getData().observe(this, result -&gt; {
          data = result;
          loading.dismiss();
      });
    }
  
    private void startCompile() {
      // trigger to start homeViewModel to read and compile all data
    }
  
    public void changePage(String page) {
      // set fragment
    }
  }  
  </code></pre>
  <p><strong>HomeViewModel</strong></p>
  <pre><code>public class HomeViewModel extends BaseViewModel {
    private final MutableLiveData&lt;Data&gt; result = new MutableLiveData&lt;&gt;();
  
    public void startCompile(Realm realm, ContentData contentData) {
      // read and compile data
      
      result.postValue(resultData);
    }
  
    public LiveData&lt;Data&gt; getData() {
      return result;
    }
  }
  </code></pre>