Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dicoding.myflexiblefragment;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.fragment.app.Fragment;
- import androidx.fragment.app.FragmentManager;
- public class DetailCategoryFragment extends Fragment implements View.OnClickListener {
- private TextView tvCategoryName;
- private TextView tvCategoryDescription;
- private Button btnProfile;
- private Button btnShowDialog;
- public static final String EXTRA_NAME = "extra_name";
- private static final String EXTRA_DESCRIPTION = "extra_description";
- private String description;
- public DetailCategoryFragment() {
- // Required empty public constructor
- }
- private String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- @Override
- public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- return inflater.inflate(R.layout.fragment_detail_category, container, false);
- }
- @Override
- public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- tvCategoryName = view.findViewById(R.id.tv_category_name);
- tvCategoryDescription = view.findViewById(R.id.tv_category_description);
- btnProfile = view.findViewById(R.id.btn_profile);
- btnProfile.setOnClickListener(this);
- btnShowDialog = view.findViewById(R.id.btn_show_dialog);
- btnShowDialog.setOnClickListener(this);
- }
- @Override
- public void onActivityCreated(@Nullable Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- if (savedInstanceState != null) {
- String descFromBundle = savedInstanceState.getString(EXTRA_DESCRIPTION);
- setDescription(descFromBundle);
- }
- if (getArguments() != null) {
- String categoryName = getArguments().getString(EXTRA_NAME);
- tvCategoryName.setText(categoryName);
- tvCategoryDescription.setText(getDescription());
- }
- }
- /*
- Gunakan method ini jika kita ingin menjaga data agar tetap aman ketika terjadi config changes (portrait - landscape)
- */
- @Override
- public void onSaveInstanceState(@NonNull Bundle outState) {
- super.onSaveInstanceState(outState);
- outState.putString(EXTRA_DESCRIPTION, getDescription());
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_profile:
- Intent mIntent = new Intent(getActivity(), ProfileActivity.class);
- startActivity(mIntent);
- break;
- case R.id.btn_show_dialog:
- OptionDialogFragment mOptionDialogFragment = new OptionDialogFragment();
- FragmentManager mFragmentManager = getChildFragmentManager();
- mOptionDialogFragment.show(mFragmentManager, OptionDialogFragment.class.getSimpleName());
- break;
- }
- }
- /*
- Kode yang akan dijalankan ketika option dialog dipilih ok
- */
- public final OptionDialogFragment.OnOptionDialogListener optionDialogListener = new OptionDialogFragment.OnOptionDialogListener() {
- @Override
- public void onOptionChosen(String text) {
- Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement