problem description
DialogFragment memory leak
the environmental background of the problems and what methods you have tried
how can I fix the secondary leak?
DialogFragment memory leak
how can I fix the secondary leak?
from this picture, the root lies in your brother's obj citation of Message, and the other citation is all WeakReference. Do you use any Handler to send messages?
solution: rewrite onActivityCreated, to set all three Listener to empty
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
dialog.setOnShowListener(null)
dialog.setOnCancelListener(null)
dialog.setOnDismissListener(null)
}
I hope to adopt it, thank you!
problem analysis: there is an onActivityCreated method in
DialogFragment. Android implements this method by default:
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (this.mShowsDialog) {
View view = this.getView();
if (view != null) {
if (view.getParent() != null) {
throw new IllegalStateException("DialogFragment can not be attached to a container view");
}
this.mDialog.setContentView(view);
}
Activity activity = this.getActivity();
if (activity != null) {
this.mDialog.setOwnerActivity(activity);
}
this.mDialog.setCancelable(this.mCancelable);
this.mDialog.setOnCancelListener(this);
this.mDialog.setOnDismissListener(this);
if (savedInstanceState != null) {
Bundle dialogState = savedInstanceState.getBundle("android:savedDialogState");
if (dialogState != null) {
this.mDialog.onRestoreInstanceState(dialogState);
}
}
}
}
SetOnCancelListener and setOnDismissListener are set by default in code
this.mDialog.setOnCancelListener(this);
this.mDialog.setOnDismissListener(this);
use setOnDismissListener to analyze, setOnCancelListener is the same:
// Dialog
private Message mDismissMessage;
// DialogmDismissMessage;
public void setOnDismissListener(@Nullable OnDismissListener listener) {
if (mCancelAndDismissTaken != null) {
throw new IllegalStateException(
"OnDismissListener is already taken by "
+ mCancelAndDismissTaken + " and can not be replaced.");
}
if (listener != null) { // listenerMessage
mDismissMessage = mListenersHandler.obtainMessage(DISMISS, listener);
} else {
mDismissMessage = null;
}
}
definition of mListenersHandler:
private static final class ListenersHandler extends Handler {
private final WeakReference<DialogInterface> mDialog;
public ListenersHandler(Dialog dialog) {
mDialog = new WeakReference<>(dialog);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DISMISS:
((OnDismissListener) msg.obj).onDismiss(mDialog.get());
break;
case CANCEL:
((OnCancelListener) msg.obj).onCancel(mDialog.get());
break;
case SHOW:
((OnShowListener) msg.obj).onShow(mDialog.get());
break;
}
}
}
ListenersHandler mainly calls the implementation of Listener through Handler when dealing with Listener.
call location:
when the pop-up window is closed
@Override
public void dismiss() {
if (Looper.myLooper() == mHandler.getLooper()) {
dismissDialog(); //
} else {
mHandler.post(mDismissAction);
}
}
void dismissDialog() {
if (mDecor == null || !mShowing) {
return;
}
if (mWindow.isDestroyed()) {
Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!");
return;
}
try {
mWindowManager.removeViewImmediate(mDecor);
} finally {
if (mActionMode != null) {
mActionMode.finish();
}
mDecor = null;
mWindow.closeAllPanels();
onStop();
mShowing = false;
sendDismissMessage(); //
}
}
private void sendDismissMessage() {
if (mDismissMessage != null) {
// Obtain a new message so this dialog can be re-used
Message.obtain(mDismissMessage).sendToTarget(); // HandlerHandlermListenersHandler(ListenersHandler)
}
}
because mDismissMessage is a global variable, obj references Listener (DialogFragment), Dialog references mDismissMessage, holds references to each other, there is a number of references, resulting in a memory leak.
if there are any problems in the analysis, please correct them, thank you.
reference: https://www.cnblogs.com/endur.
@ Coolspan Brother's use is wrong. If set to empty, the displayed Dialog will be redisplayed after Activity returns to the background and then re-enters!
focus on the setLoadMoreData (String json) method. Class Code: public class WatchHistoryActivity extends BaseActivity implements View.OnClickListener, BaseQuickAdapter.RequestLoadMoreListener { @BindView(R.id.rv_watch_history) RecyclerView mR...
1. Pages on the Android simulator cannot be scanned 2. Error loading pictures and small icons on Android simulator 3. The navigation bar cannot be displayed Everything that comes out of npm start can be displayed weex run android I just got in...
Android Studio 3.0. useless code can be found. But the resources just don t show No suspicious code found does anyone have the same problem as me ...
running react-native run-android will report the error as above. Real usb debugging has been turned on and connected. Is it a problem with Android environment configuration? ...
question: execute. gradlew assembleRelease, prompt build questions! You can also see that the app release version of apk is generated. When I open it, there is a flicker (stop running) previous status: can be run before, but it can be run after be...
android developer. There is an ImageView control in ScrollView. I want the user to hide what I should do three seconds after seeing it. I don t know how to judge that the user has seen it. Ask for advice ...
use the video frame Vitamio to report an error: Project with path : vitamio could not be found in project : app . Here are the steps for me to import Moudle: this is the unzipped file downloaded from the official website. It is said on the Interne...
after popping up, window needs to be close to RadioGroup, not to the top of the screen. layout: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http: schemas.android.com apk res android" ...
is there any way to see a method called in the system source code, such as the annotation processor process () ...
now, does the mobile phone adapt to the ratio that the mobile end is 18:9? is there a feasible scheme ...
1. Failed to upload pictures using Qiniu on Android devices 2. Prompt Unable to resolve host "up.qiniu.com ": No address associated with hostname; Connect to 183.136.139.16 timed out 80 timed out 3. The old code used before is used, and the previous...
The page does the function of the page before the jump after the completion of the event, using the method of adding returnUrl on Url, but when testing on the mobile side, a pop-up window will appear to show the path of the page to be redirected before r...
when you hit the android package locally and debug in chrome: inspect -sharpdevices or GapDeBug, there will be an exception in the debugging interface, unable to view breakpoints, etc., and the debugging interface for debugging other app application pa...
how to change Android SDK Platform-Tools27.0.1 to 23.0.1 ...
as the title: Android is like the kind of connotative jokes, drop-down refresh, pull-up and load constantly get new data, and will not repeat ...
existing requirements for ListView item to support gesture zooming in and out, trying some of what is said online is unsuccessful, how should it be realized? ...
WINDOWS for ionic+android development error installing android platform support error message is as follows: D: projects MyIonicProject>ionic cordova platform add android > cordova platform add android --save Using cordova-fetch for cordova...
see the following changes in Android P Behavior Changes FLAG_ACTIVITY_NEW_TASK requirement is now enforced With Android P, you cannot start an activity from a non-activity context unless you pass the intent flag FLAG_ACTIVITY_NEW_TASK. If you attem...
this is something in chrome after I turned on debug mode on my phone. Now I want to debug version 53 of webview, instead of using chrome to open the web page to see the effect. I want to know if there are any js errors in the web page. ...
vue s project opens to the login screen on Android. But what is not shown in the login screen, only the background color written in app.vue is displayed. I don t know if this is a compatibility issue or a page layout and styling issue I wrote, but this...