Skip to content

Commit 3b5aa02

Browse files
committed
see 11/03 log
1 parent a64c2e0 commit 3b5aa02

File tree

5 files changed

+32
-47
lines changed

5 files changed

+32
-47
lines changed

app/src/main/java/com/blankj/androidutilcode/activities/SnackbarActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
3434
findViewById(R.id.btn_indefinite_snackbar_with_action).setOnClickListener(this);
3535
findViewById(R.id.btn_add_view).setOnClickListener(this);
3636
findViewById(R.id.btn_add_view_with_action).setOnClickListener(this);
37+
findViewById(R.id.btn_cancel_snackbar).setOnClickListener(this);
3738
}
3839

3940
@Override

update_log.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
###
2+
#### 16/11/03 SnackbarUtils中Snackbar持有弱引用来消除内存泄漏
23
#### 16/11/02 内存泄漏检测中
34
#### 16/11/01 内存泄漏检测中
45
#### 16/10/31 完善发布版本1.3.1和1.3.2

utilcode/src/main/java/com/blankj/utilcode/utils/NetworkUtils.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -363,24 +363,6 @@ public static String getIPAddress(boolean useIPv4) {
363363
return null;
364364
}
365365

366-
public String getIpAdress(Context context) {
367-
try {
368-
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
369-
en.hasMoreElements(); ) {
370-
NetworkInterface intf = en.nextElement();
371-
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
372-
InetAddress inetAddress = enumIpAddr.nextElement();
373-
if (!inetAddress.isLoopbackAddress()) {
374-
return inetAddress.getHostAddress();
375-
}
376-
}
377-
}
378-
} catch (Exception ex) {
379-
ex.printStackTrace();
380-
}
381-
return null;
382-
}
383-
384366
/**
385367
* 获取域名ip地址
386368
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>

utilcode/src/main/java/com/blankj/utilcode/utils/SnackbarUtils.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
import com.blankj.utilcode.R;
1111

12+
import java.lang.ref.ReferenceQueue;
13+
import java.lang.ref.WeakReference;
14+
1215
/**
1316
* <pre>
1417
* author: Blankj
1518
* blog : http://blankj.com
1619
* time : 2016/10/16
17-
* desc : SnackBar相关工具类
20+
* desc : Snackbar相关工具类
1821
* </pre>
1922
*/
2023
public class SnackbarUtils {
@@ -23,7 +26,7 @@ private SnackbarUtils() {
2326
throw new UnsupportedOperationException("u can't instantiate me...");
2427
}
2528

26-
private static Snackbar snackbar;
29+
private static WeakReference<Snackbar> snackbarWeakReference;
2730

2831
/**
2932
* 显示短时snackbar
@@ -69,10 +72,10 @@ public static void showLongSnackbar(View parent, CharSequence text, int textColo
6972
/**
7073
* 显示长时snackbar
7174
*
72-
* @param parent 视图(CoordinatorLayout或者DecorView)
73-
* @param text 文本
74-
* @param textColor 文本颜色
75-
* @param bgColor 背景色
75+
* @param parent 视图(CoordinatorLayout或者DecorView)
76+
* @param text 文本
77+
* @param textColor 文本颜色
78+
* @param bgColor 背景色
7679
* @param actionText 事件文本
7780
* @param actionTextColor 事件文本颜色
7881
* @param listener 监听器
@@ -132,19 +135,19 @@ private static void showSnackbar(View parent, CharSequence text, int duration, i
132135
default:
133136
case Snackbar.LENGTH_SHORT:
134137
case Snackbar.LENGTH_LONG:
135-
snackbar = Snackbar.make(parent, text, duration);
138+
snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, text, duration));
136139
break;
137140
case Snackbar.LENGTH_INDEFINITE:
138-
snackbar = Snackbar.make(parent, text, Snackbar.LENGTH_INDEFINITE).setDuration(duration);
141+
snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, text, Snackbar.LENGTH_INDEFINITE).setDuration(duration));
139142
}
140-
View view = snackbar.getView();
143+
View view = snackbarWeakReference.get().getView();
141144
((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(textColor);
142145
view.setBackgroundColor(bgColor);
143146
if (actionText != null && actionText.length() > 0 && listener != null) {
144-
snackbar.setActionTextColor(actionTextColor);
145-
snackbar.setAction(actionText, listener);
147+
snackbarWeakReference.get().setActionTextColor(actionTextColor);
148+
snackbarWeakReference.get().setAction(actionText, listener);
146149
}
147-
snackbar.show();
150+
snackbarWeakReference.get().show();
148151
}
149152

150153
/**
@@ -155,23 +158,26 @@ private static void showSnackbar(View parent, CharSequence text, int duration, i
155158
* @param index 位置(the position at which to add the child or -1 to add last)
156159
*/
157160
public static void addView(int layoutId, int index) {
158-
View view = snackbar.getView();
159-
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) view;
160-
View child = LayoutInflater.from(view.getContext()).inflate(layoutId, null);
161-
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
162-
LinearLayout.LayoutParams.WRAP_CONTENT,
163-
LinearLayout.LayoutParams.WRAP_CONTENT);
164-
params.gravity = Gravity.CENTER_VERTICAL;
165-
layout.addView(child, index, params);
161+
Snackbar snackbar = snackbarWeakReference.get();
162+
if (snackbar != null) {
163+
View view = snackbar.getView();
164+
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) view;
165+
View child = LayoutInflater.from(view.getContext()).inflate(layoutId, null);
166+
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
167+
LinearLayout.LayoutParams.WRAP_CONTENT,
168+
LinearLayout.LayoutParams.WRAP_CONTENT);
169+
params.gravity = Gravity.CENTER_VERTICAL;
170+
layout.addView(child, index, params);
171+
}
166172
}
167173

168174
/**
169175
* 取消snackbar显示
170176
*/
171177
public static void dismissSnackbar() {
172-
if (snackbar != null) {
173-
snackbar.dismiss();
174-
snackbar = null;
178+
if (snackbarWeakReference != null && snackbarWeakReference.get() != null) {
179+
snackbarWeakReference.get().dismiss();
180+
snackbarWeakReference = null;
175181
}
176182
}
177183
}

utilcode/src/test/java/com/blankj/utilcode/utils/TestUtils.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.blankj.utilcode.utils;
22

33
import android.content.Context;
4+
import android.util.SparseArray;
45

56
import org.junit.Test;
67
import org.junit.runner.RunWith;
@@ -9,13 +10,7 @@
910
import org.robolectric.annotation.Config;
1011

1112
import java.io.File;
12-
import java.lang.reflect.Array;
13-
import java.util.Arrays;
1413
import java.util.List;
15-
import java.util.regex.Pattern;
16-
17-
import static com.blankj.utilcode.utils.TimeUtils.milliseconds2String;
18-
import static com.google.common.truth.Truth.assertThat;
1914

2015
/**
2116
* <pre>

0 commit comments

Comments
 (0)