Setting visibility of button when back pressed from a fragment

209
May 04, 2018, at 12:01 PM

I have an activity where the user enters some data and submits it via a submit button, that same activity also has some more buttons which lead to fragments.

When I click on the fragments, the submit button of the activity overlaps the UI of the fragment, so in the calling code of fragments I set the visibility of that submit button as invisible but when back pressed from fragment then also that submit button is invisible.

I want the submit button to be visible when activity is being displayed and invisible when the fragments are being displayed.

Answer 1

Add your fragment on button click,

loginSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            loginSubmit.setVisibility(View.GONE);
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.addToBackStack("LayoutFragment");
            ft.add(R.id.framelayoutfaqs, new LayoutFragment());
            ft.commit();
        }
    });
@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStackImmediate();
        loginSubmit.setVisibility(View.VISIBLE);
    } else
        super.onBackPressed();
}

NOTE : make sure you import same Fragment class which you used to create YourFragment. Also choose getSupportFragmentManager() or getFragmentManager() accordingly.

Answer 2

You can handle it in 0nBackPressed() method of Activity class .

Answer 3

Make the button invisible in either of onCreate, onActivityCreated or onAttach methods of your fragment and make it visible in onDetach method of your fragment.

Rent Charter Buses Company
READ ALSO
How can get CRC from android ble scan result?

How can get CRC from android ble scan result?

I have receive scan result with 62 bytes each and every time from ble device

227
How to create separate folder for captured images with losing quality

How to create separate folder for captured images with losing quality

Hi i am trying to create separate folder for captured images with out losing quality using below code but i am getting exception androidos

164