I believe strongly removing the Listener, referenced by a View object, in onDestroy
() is too late. This override method occurs after onDestroyView
(), which supposed to "...clean up resources associated with its View."
You may use the same code in onStop()
instead. Although I did not use this technique.
I can suggest this code, which I have used without any issues with the debugger.
// Code below is an example. Please change it to code that is more applicable to your app.
final View myView = rootView.findViewById(R.id.myView);
myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi") @SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
// Obtain layout data from view...
int w = myView.getWidth();
int h = myView.getHeight();
// ...etc.
// Once data has been obtained, this listener is no longer needed, so remove it...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
myView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
else {
myView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
Notes:
- Since
getViewTreeObserver
is used for layouts, normally you need this listener for a short time only. Hence the listener is removed immediately.
- The second call to
removeOnGlobalLayoutListener
() should be crossed out by the Studio since it is not available before JELLY_BEAN.
- Pragma code
@SuppressWarnings("deprecation")
is not necessary if you're using Android Studio.
- Code
myView = rootView.findViewById(R.id.myView);
may need to change to a more applicable code to your app or situation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…