title: How to Share Data between Activity and Fragment via ViewModel Class in Android?
body: <p>I wondered if it's possible to pass a String data which has declared in Activity class and pass the String data to ViewModel class then pass the data to Fragment class.</p>
  <p><strong>ViewModel Class</strong></p>
  <pre class="lang-java prettyprint-override"><code>class TimeTableViewModel extends ViewModel {
  
  private MutableLiveData&lt;String&gt; start_time_str = new MutableLiveData&lt;&gt;();
  
  void send_StartTime(String start_Time){
      start_time_str.setValue(start_Time);
  }
  
  LiveData&lt;String&gt; get_StartTime(){
      return start_time_str;
  }}
  </code></pre>
  <p>In ViewModel Class, I have <code>MutableLiveData&lt;String&gt; start_time_str</code> and it has been initialized as <code>new MutableLiveData&lt;&gt;();</code></p>
  <p>I would like to use <code>void send_StartTime(String start_Time)</code> function in Activity class to set value of argument <code>String start_Time</code> and call the <code>start_time_str</code> in Fragment class.</p>
  <p><strong>Activity Class</strong></p>
  <pre class="lang-java prettyprint-override"><code>@Override
  public boolean onOptionsItemSelected(MenuItem item){
      switch (item.getItemId()){
          case android.R.id.home:
              finish();
              break;
          case R.id.add_schedule_save:
              String start_time_str = startTime.getText().toString();
              Intent intent_restart0 = new Intent(TimeTable_Add_New_Schedule.this, MainActivity.class);
              startActivity(intent_restart0);
              TimeTableViewModel timeTableViewModel = new TimeTableViewModel();
              timeTableViewModel.send_StartTime(start_time_str);
              Toast.makeText(this,&quot;&quot;+start_time_str,Toast.LENGTH_LONG).show();
              break;
      }
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  <p><strong>Fragment Class</strong></p>
  <pre class="lang-java prettyprint-override"><code>@Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      TimeTableViewModel timeTableViewModel = new TimeTableViewModel();
      timeTableViewModel.get_StartTime().observe(getViewLifecycleOwner(), new Observer&lt;String&gt;() {
          @Override
          public void onChanged(String s) {
              mon_textView_11.setText(s);
          }
      });
  }
  </code></pre>
  <p>In the Fragment class I call <code>get_StartTime()</code> function to get <code>start_time_str</code> and set the String value to my TextView. I think the <code>start_time_str</code> has been successfully set by function of <code>timeTableViewModel.send_StartTime(start_time_str);</code> in the Activity  Class because of Toast.maketext is worked like a charm. <strong>However the TextView is not shown anything. I have tested Text Color is not white so that if the string value is correctly called, it should be appear on screen.</strong> If you have any suggestions, I would love to hear your advice.
  Thank you very much.</p>
  <p><a href="https://i.stack.imgur.com/6Nxyu.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/6Nxyu.gif" alt="enter image description here" /></a></p>