Advertisement
dhohoney

DetailCategoryFragment

Mar 30th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package com.dicoding.myflexiblefragment;
  2.  
  3.  
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import androidx.annotation.NonNull;
  14. import androidx.annotation.Nullable;
  15. import androidx.fragment.app.Fragment;
  16. import androidx.fragment.app.FragmentManager;
  17.  
  18.  
  19. public class DetailCategoryFragment extends Fragment implements View.OnClickListener {
  20.  
  21. private TextView tvCategoryName;
  22. private TextView tvCategoryDescription;
  23. private Button btnProfile;
  24. private Button btnShowDialog;
  25.  
  26. public static final String EXTRA_NAME = "extra_name";
  27. private static final String EXTRA_DESCRIPTION = "extra_description";
  28. private String description;
  29.  
  30. public DetailCategoryFragment() {
  31. // Required empty public constructor
  32. }
  33.  
  34. private String getDescription() {
  35. return description;
  36. }
  37.  
  38. public void setDescription(String description) {
  39. this.description = description;
  40. }
  41.  
  42. @Override
  43. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
  44. Bundle savedInstanceState) {
  45. // Inflate the layout for this fragment
  46. return inflater.inflate(R.layout.fragment_detail_category, container, false);
  47. }
  48.  
  49. @Override
  50. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  51. super.onViewCreated(view, savedInstanceState);
  52. tvCategoryName = view.findViewById(R.id.tv_category_name);
  53. tvCategoryDescription = view.findViewById(R.id.tv_category_description);
  54. btnProfile = view.findViewById(R.id.btn_profile);
  55. btnProfile.setOnClickListener(this);
  56. btnShowDialog = view.findViewById(R.id.btn_show_dialog);
  57. btnShowDialog.setOnClickListener(this);
  58. }
  59.  
  60. @Override
  61. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  62. super.onActivityCreated(savedInstanceState);
  63.  
  64. if (savedInstanceState != null) {
  65. String descFromBundle = savedInstanceState.getString(EXTRA_DESCRIPTION);
  66. setDescription(descFromBundle);
  67. }
  68.  
  69. if (getArguments() != null) {
  70. String categoryName = getArguments().getString(EXTRA_NAME);
  71. tvCategoryName.setText(categoryName);
  72. tvCategoryDescription.setText(getDescription());
  73.  
  74. }
  75. }
  76.  
  77. /*
  78. Gunakan method ini jika kita ingin menjaga data agar tetap aman ketika terjadi config changes (portrait - landscape)
  79. */
  80. @Override
  81. public void onSaveInstanceState(@NonNull Bundle outState) {
  82. super.onSaveInstanceState(outState);
  83.  
  84. outState.putString(EXTRA_DESCRIPTION, getDescription());
  85. }
  86.  
  87. @Override
  88. public void onClick(View v) {
  89. switch (v.getId()) {
  90. case R.id.btn_profile:
  91. Intent mIntent = new Intent(getActivity(), ProfileActivity.class);
  92. startActivity(mIntent);
  93. break;
  94.  
  95. case R.id.btn_show_dialog:
  96. OptionDialogFragment mOptionDialogFragment = new OptionDialogFragment();
  97.  
  98. FragmentManager mFragmentManager = getChildFragmentManager();
  99. mOptionDialogFragment.show(mFragmentManager, OptionDialogFragment.class.getSimpleName());
  100. break;
  101. }
  102. }
  103.  
  104. /*
  105. Kode yang akan dijalankan ketika option dialog dipilih ok
  106. */
  107. public final OptionDialogFragment.OnOptionDialogListener optionDialogListener = new OptionDialogFragment.OnOptionDialogListener() {
  108. @Override
  109. public void onOptionChosen(String text) {
  110. Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
  111. }
  112. };
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement