Search code examples
androidmaterial-designbottom-sheetandroid-bottomappbar

Refresh Activity when item select in BottomSheet Fragment


I have set up a BottomAppBar with a NavigationDrawer but I can't recover the item selected in this Drawer which is a fragment.

I followed many tutorials to set up this BottomAppBar but at no time did I see how to get the selection from the Drawer.

Here is how my Drawer is created:

public class MaintainerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintainer);

        setUpBottomAppBar();
    }

    private void loadFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.main_container, fragment);
        transaction.commit();
    }

    private void setUpBottomAppBar() {
        BottomAppBar bottomAppBar = findViewById(R.id.bottom_app_bar);
        setSupportActionBar(bottomAppBar);

        bottomAppBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BottomSheetDialogFragment bottomSheetDialogFragment = BottomSheetNavigationMaintainerFragment.newInstance();
                bottomSheetDialogFragment.show(getSupportFragmentManager(), "Bottom Sheet Dialog Fragment");
                Bundle args = bottomSheetDialogFragment.getArguments();
            }
        });

        bottomAppBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
}

Finally, here is the code of my fragment:

public class BottomSheetNavigationMaintainerFragment extends BottomSheetDialogFragment {

    public BottomSheetNavigationMaintainerFragment() {
        // Required empty public constructor
    }

    public static BottomSheetNavigationMaintainerFragment newInstance() {
        Bundle args = new Bundle();
        BottomSheetNavigationMaintainerFragment fragment = new BottomSheetNavigationMaintainerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) { }
    };

    @SuppressLint("RestrictedApi")
    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        //Get the content View
        View contentView = View.inflate(getContext(), R.layout.fragment_bottom_navigation_maintainer, null);
        dialog.setContentView(contentView);

        NavigationView navigationView = contentView.findViewById(R.id.navigation_view);

        //implement navigation menu item click event
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.app_bar_assets:
                        break;
                    case R.id.app_bar_items:
                        break;
                }
                return false;
            }
        });
        ImageView closeButton = contentView.findViewById(R.id.close_image_view);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //dismiss bottom sheet
                dismiss();
            }
        });

        //Set the coordinator layout behavior
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
        CoordinatorLayout.Behavior behavior = params.getBehavior();

        //Set callback
        if (behavior instanceof BottomSheetBehavior) {
            ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
        }
    }
}

I would therefore like to return the selected item in the onNavigationItemSelected method but I can't find a way to link this fragment to my activity in which it is created


Solution

  • I am adding one fragment in activity. in which fragment I call bottomFragment on button click open a bottom sheet and on click of bottom bar text the toast print in the fragment. Create Dialog class with a click event interface

      public class AutoAppSelectrDialogFragment extends BottomSheetDialogFragment {
    
            OnAutoAppSelectPickerListener mListener;
    
            /**
             * Factory method.
             *
             * @return A new instance of fragment ImagePickerDialogFragment.
             */
            public static AutoAppSelectrDialogFragment newInstance(boolean showRemove) {
                AutoAppSelectrDialogFragment fragment = new AutoAppSelectrDialogFragment();
                Bundle bundle = new Bundle();
                bundle.putBoolean("showRemove", showRemove);
                fragment.setArguments(bundle);
                return fragment;
            }
    
            @SuppressLint("RestrictedApi")
            @Override
            public void setupDialog(final Dialog dialog, int style) {
                super.setupDialog(dialog, style);
                View contentView = View.inflate(getContext(), R.layout.bottom_sheet_auto_run, null);
                ButterKnife.bind(this, contentView);
                dialog.setCanceledOnTouchOutside(false);
                dialog.setContentView(contentView);
            }
    
            public void setListener(OnAutoAppSelectPickerListener listener) {
                mListener = listener;
            }
    
    
            @OnClick(R.id.txt_1)
            public void Click1() {
                if (mListener != null)
                    mListener.onClick1();
                dismiss();
            }
    
            @OnClick(R.id.txt_2)
            public void Click2() {
                if (mListener != null)
                    mListener.onClick2();
                dismiss();
            }
    
    
    
    
            public interface OnAutoAppSelectPickerListener {
                void onClick1();
                void onClick2();
            }
    
        }
    

    XML of dialogue Fragment layout bottom_sheet_auto_run

    <TextView
                    android:id="@+id/txt_1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:background="?actionBarItemBackground"
                    android:gravity="center"
                    android:padding="@dimen/dp_12"
                    android:textColor="@color/content_text_color"
                    android:textSize="@dimen/font_16_sp" />
    
    <TextView
                    android:id="@+id/txt_2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:background="?actionBarItemBackground"
                    android:gravity="center"
                    android:padding="@dimen/dp_12"
                    android:textColor="@color/content_text_color"
                    android:textSize="@dimen/font_16_sp" />
    

    Open Bottom Sheet Dialogue on click of a button in fragment or activity like this and using a interface to implement an on click event of DialougeFragment layout

        public class AutoSartInformation extends Fragment implements AutoAppSelectrDialogFragment.OnAutoAppSelectPickerListener {
          @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // Inflate the layout for this fragment
        imgAutoStartApp = view.findViewById(R.id.img_auto_start_app);
         imgAutoStartApp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AutoAppSelectrDialogFragment imagePickerDialogFragment = AutoAppSelectrDialogFragment.newInstance(true);
                        imagePickerDialogFragment.setCancelable(false);
                        imagePickerDialogFragment.setListener(AutoSartInformation.this);
                        imagePickerDialogFragment.show(getActivity().getSupportFragmentManager(), imagePickerDialogFragment.getTag());
                    }
                });
           }
    
    
        @Override
        public void onClick1() {
          Log.d("Click","1");
    open Fragment on click of dialouge first text click
        }
    
        @Override
        public void onClick2() {
            Log.d("Click","2");
        }