JustABeginer
JustABeginer

Reputation: 3

android : Should we call onPause & onResume in onHiddenChanged function?

We are using the fragment without viewpage, just simply press the button to hide fragment A show fragment B or show A hide B, it will trigger the onHiddenChanged() function

But we have a argument here, one would like to run onPause() & onResume() directly in onHiddenChanged() just like running the activity

  @Override
    public void onHiddenChanged(boolean b) {
        super.onHiddenChanged(b);
        if (b) {
            onPause();
        } else {
            onResume();
        }
    }

one was worry about changing the fragment lifecycle may cause the another problem, prefers not touching the onPause() & onResume()

  @Override
    public void onHiddenChanged(boolean b) {
        super.onHiddenChanged(b);
        if (b) {
            //run onfragmenthide;
        } else {
            //run onframgnetshow;
        }
    }

my question is

  1. should we running onPause() & onResume() in onHiddenChanged()?
  2. if we run it, are kind of detail we should noted for?

Upvotes: 0

Views: 75

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199795

No, you should absolutely not do that as you must not call into super.onPause() and super.onResume() unless the system is calling those methods.

Instead, extract your logic into two separate methods that you call in both places after the respective super methods.

Upvotes: 1

Related Questions