-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToaster.java
40 lines (31 loc) · 1.04 KB
/
Toaster.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import android.content.Context;
import android.widget.Toast;
import androidx.annotation.NonNull;
import java.lang.ref.WeakReference;
public class Toaster {
private static Toaster toaster;
private static android.widget.Toast toast;
private final WeakReference<Context> weakReference;
private Toaster(@NonNull Context context) {
weakReference = new WeakReference<>(context);
}
public static Toaster getInstance(@NonNull Context context) {
if (toaster == null) {
toaster = new Toaster(context.getApplicationContext());
}
return toaster;
}
public void showShort(@NonNull String message) {
show(message, false);
}
public void showLong(@NonNull String message) {
show(message, true);
}
private void show(@NonNull String message, boolean _long) {
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(weakReference.get(), message, _long ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
toast.show();
}
}