Skip to content

Instantly share code, notes, and snippets.

@tjrkdgnl
Created August 19, 2020 07:36
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ViewModelPractice viewModelPractice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewModelPractice = new ViewModelProvider(this).get(ViewModelPractice.class);
Fragment fg = new FragmentPractice();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.fragment_container,fg).commit();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.change_value_button :
viewModelPractice.setValueOfLiveData(viewModelPractice.getValue()+1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment