简体   繁体   中英

Setting visibility of button when back pressed from a fragment

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.

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.

您可以在Activity类的0nBackPressed()方法中进行处理。

使按钮在片段的onCreateonActivityCreatedonAttach方法中不可见,并使其在片段的onDetach方法中可见。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM