Skip to content

Commit

Permalink
新增支持设置 Toast 跨页面显示
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Sep 24, 2023
1 parent 17adbca commit 5804d7c
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 38 deletions.
13 changes: 13 additions & 0 deletions HelpDoc-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

* [What should I do if the framework cannot meet the scene I am currently using](#what-should-i-do-if-the-framework-cannot-meet-the-scene-i-am-currently-using)

* [How to implement cross page display of toast in the framework](#how-to-implement-cross-page-display-of-toast-in-the-framework)

* [Why the framework prefers to use window manager to implement toast](#why-the-framework-prefers-to-use-window-manager-to-implement-toast)

#### How to customize toast display animation
Expand Down Expand Up @@ -146,6 +148,17 @@ public class ToastStrategy {

* The Toaster framework is intended to solve some toast requirements. If Toaster cannot meet your needs, you can consider using the [EasyWindow](https://github.com/getActivity/EasyWindow) floating window framework to achieve it.

#### How to implement cross page display of toast in the framework

* By default, Toasts in the Toaster framework are only displayed on the current Activity. If an Activity switch occurs, the Toast becomes invisible as the current Activity is destroyed, making it impossible to display on the new Activity. The framework added support for this feature in version [12.5](https://github.com/getActivity/Toaster/releases/tag/12.5). The following is an example of how to use it:

```java
ToastParams params = new ToastParams();
params.text = "I am a Toast that can be displayed across pages";
// Indicates that this Toast needs to be displayed across pages
params.crossPageShow = true;
Toaster.show(params);

#### Why the framework prefers to use window manager to implement toast

* There are too many pits in the system toast, the main problems are as follows:
Expand Down
14 changes: 14 additions & 0 deletions HelpDoc-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

* [框架无法满足我当前使用的场景怎么办](#框架无法满足我当前使用的场景怎么办)

* [框架中的 Toast 如何实现跨页面显示](#框架中的-toast-如何实现跨页面显示)

* [为什么框架优先使用 WindowManager 来实现 Toast](#为什么框架优先使用-windowManager-来实现-toast)

#### 框架怎么改名了
Expand Down Expand Up @@ -162,6 +164,18 @@ public class ToastStrategy {

* Toaster 框架意在解决一些的 Toast 需求,如果 Toaster 无法满足你的需求,你可以考虑使用 [EasyWindow](https://github.com/getActivity/EasyWindow) 悬浮窗框架来实现。

#### 框架中的 Toast 如何实现跨页面显示

* Toaster 中默认 Toast 是只在当前 Activity 上面展示的,如果遇到 Activity 切换,那么在 Toast 随着当前 Activity 销毁而不可见,导致无法在新 Activity 上面展示,框架在 [12.5](https://github.com/getActivity/Toaster/releases/tag/12.5) 版本新增支持了这一功能,具体调用示例如下:

```java
ToastParams params = new ToastParams();
params.text = "我是一个能跨页面展示的 Toast";
// 表示这个 Toast 需要跨页面展示
params.crossPageShow = true;
Toaster.show(params);
```

#### 为什么框架优先使用 WindowManager 来实现 Toast

* 系统 Toast 的坑太多了,主要问题表现如下:
Expand Down
18 changes: 15 additions & 3 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

* Project address: [Github](https://github.com/getActivity/Toaster)

* [Click here to download demo apk directly](https://github.com/getActivity/Toaster/releases/download/12.3/Toaster.apk)
* [Click here to download demo apk directly](https://github.com/getActivity/Toaster/releases/download/12.5/Toaster.apk)

![](picture/en/demo_toast_activity.jpg) ![](picture/en/demo_toast_style_white.jpg) ![](picture/en/demo_toast_style_black.jpg)

Expand Down Expand Up @@ -49,7 +49,7 @@ android {
dependencies {
// Toast framework:https://github.com/getActivity/Toaster
implementation 'com.github.getActivity:Toaster:12.3'
implementation 'com.github.getActivity:Toaster:12.5'
}
```

Expand Down Expand Up @@ -128,13 +128,21 @@ Toaster.setInterceptor(IToastInterceptor interceptor);
Toaster.getInterceptor();
```

#### Framework proguard rules

* Added in the `proguard-rules.pro` file:

```text
-keep class com.hjq.toast.** {*;}
```

## [Please click here to view frequently asked questions](HelpDoc-en.md)

#### Comparison between different Toast frameworks

| Function or detail | [Toaster](https://github.com/getActivity/Toaster) |[ AndroidUtilCode-ToastUtils ](https://github.com/Blankj/AndroidUtilCode)| [Toasty](https://github.com/GrenderG/Toasty) |
| :----: | :------: | :-----: | :-----: |
| Corresponding version | 12.3 | 1.30.6 | 1.5.0 |
| Corresponding version | 12.5 | 1.30.6 | 1.5.0 |
| Number of issues | [![](https://img.shields.io/github/issues/getActivity/Toaster.svg)](https://github.com/getActivity/Toaster/issues) |[![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/issues)| [![](https://img.shields.io/github/issues/GrenderG/Toasty.svg)](https://github.com/GrenderG/Toasty/issues) |
| Framework pack size | 31 KB | 500 KB | 50 KB |
| Framework maintenance status| 维护中 | 停止维护 | 停止维护 |
Expand Down Expand Up @@ -238,12 +246,16 @@ new Toast

* Shape view framework: [ShapeView](https://github.com/getActivity/ShapeView)![](https://img.shields.io/github/stars/getActivity/ShapeView.svg)![](https://img.shields.io/github/forks/getActivity/ShapeView.svg)

* Shape drawable framework: [ShapeDrawable](https://github.com/getActivity/ShapeDrawable)![](https://img.shields.io/github/stars/getActivity/ShapeDrawable.svg)![](https://img.shields.io/github/forks/getActivity/ShapeDrawable.svg)

* Language switching framework: [Multi Languages](https://github.com/getActivity/MultiLanguages)![](https://img.shields.io/github/stars/getActivity/MultiLanguages.svg)![](https://img.shields.io/github/forks/getActivity/MultiLanguages.svg)

* Gson parsing fault tolerance: [GsonFactory](https://github.com/getActivity/GsonFactory)![](https://img.shields.io/github/stars/getActivity/GsonFactory.svg)![](https://img.shields.io/github/forks/getActivity/GsonFactory.svg)

* Logcat viewing framework: [Logcat](https://github.com/getActivity/Logcat)![](https://img.shields.io/github/stars/getActivity/Logcat.svg)![](https://img.shields.io/github/forks/getActivity/Logcat.svg)

* Nested scrolling layout framework:[NestedScrollLayout](https://github.com/getActivity/NestedScrollLayout) ![](https://img.shields.io/github/stars/getActivity/NestedScrollLayout.svg) ![](https://img.shields.io/github/forks/getActivity/NestedScrollLayout.svg)

* Android version guide: [AndroidVersionAdapter](https://github.com/getActivity/AndroidVersionAdapter)![](https://img.shields.io/github/stars/getActivity/AndroidVersionAdapter.svg)![](https://img.shields.io/github/forks/getActivity/AndroidVersionAdapter.svg)

* Android code standard: [AndroidCodeStandard](https://github.com/getActivity/AndroidCodeStandard)![](https://img.shields.io/github/stars/getActivity/AndroidCodeStandard.svg)![](https://img.shields.io/github/forks/getActivity/AndroidCodeStandard.svg)
Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

* 博客地址:[只需体验三分钟,你就会跟我一样,爱上这款 Toast](https://www.jianshu.com/p/9b174ee2c571)

* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Toaster/releases/download/12.3/Toaster.apk)
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/Toaster/releases/download/12.5/Toaster.apk)

![](picture/zh/download_demo_apk_qr_code.png)

Expand Down Expand Up @@ -53,7 +53,7 @@ android {
dependencies {
// 吐司框架:https://github.com/getActivity/Toaster
implementation 'com.github.getActivity:Toaster:12.3'
implementation 'com.github.getActivity:Toaster:12.5'
}
```

Expand Down Expand Up @@ -132,13 +132,21 @@ Toaster.setInterceptor(IToastInterceptor interceptor);
Toaster.getInterceptor();
```

#### 框架混淆规则

* 在混淆规则文件 `proguard-rules.pro` 中加入

```text
-keep class com.hjq.toast.** {*;}
```

## [常见疑问请点击此处查看](HelpDoc-zh.md)

#### 不同 Toast 框架之间的对比

| 功能或细节 | [Toaster](https://github.com/getActivity/Toaster) | [AndroidUtilCode-ToastUtils](https://github.com/Blankj/AndroidUtilCode) | [Toasty](https://github.com/GrenderG/Toasty) |
| :----: | :------: | :-----: | :-----: |
| 对应版本 | 12.3 | 1.30.6 | 1.5.0 |
| 对应版本 | 12.5 | 1.30.6 | 1.5.0 |
| issues 数 | [![](https://img.shields.io/github/issues/getActivity/Toaster.svg)](https://github.com/getActivity/Toaster/issues) | [![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/issues) | [![](https://img.shields.io/github/issues/GrenderG/Toasty.svg)](https://github.com/GrenderG/Toasty/issues) |
| 框架体积 | 32 KB | 500 KB | 50 KB |
| 框架维护状态 | 维护中 | 停止维护 | 停止维护 |
Expand Down Expand Up @@ -244,14 +252,18 @@ new Toast

* 悬浮窗框架:[EasyWindow](https://github.com/getActivity/EasyWindow) ![](https://img.shields.io/github/stars/getActivity/EasyWindow.svg) ![](https://img.shields.io/github/forks/getActivity/EasyWindow.svg)

* Shape 框架:[ShapeView](https://github.com/getActivity/ShapeView) ![](https://img.shields.io/github/stars/getActivity/ShapeView.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeView.svg)
* ShapeView 框架:[ShapeView](https://github.com/getActivity/ShapeView) ![](https://img.shields.io/github/stars/getActivity/ShapeView.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeView.svg)

* ShapeDrawable 框架:[ShapeDrawable](https://github.com/getActivity/ShapeDrawable) ![](https://img.shields.io/github/stars/getActivity/ShapeDrawable.svg) ![](https://img.shields.io/github/forks/getActivity/ShapeDrawable.svg)

* 语种切换框架:[MultiLanguages](https://github.com/getActivity/MultiLanguages) ![](https://img.shields.io/github/stars/getActivity/MultiLanguages.svg) ![](https://img.shields.io/github/forks/getActivity/MultiLanguages.svg)

* Gson 解析容错:[GsonFactory](https://github.com/getActivity/GsonFactory) ![](https://img.shields.io/github/stars/getActivity/GsonFactory.svg) ![](https://img.shields.io/github/forks/getActivity/GsonFactory.svg)

* 日志查看框架:[Logcat](https://github.com/getActivity/Logcat) ![](https://img.shields.io/github/stars/getActivity/Logcat.svg) ![](https://img.shields.io/github/forks/getActivity/Logcat.svg)

* 嵌套滚动布局框架:[NestedScrollLayout](https://github.com/getActivity/NestedScrollLayout) ![](https://img.shields.io/github/stars/getActivity/NestedScrollLayout.svg) ![](https://img.shields.io/github/forks/getActivity/NestedScrollLayout.svg)

* Android 版本适配:[AndroidVersionAdapter](https://github.com/getActivity/AndroidVersionAdapter) ![](https://img.shields.io/github/stars/getActivity/AndroidVersionAdapter.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidVersionAdapter.svg)

* Android 代码规范:[AndroidCodeStandard](https://github.com/getActivity/AndroidCodeStandard) ![](https://img.shields.io/github/stars/getActivity/AndroidCodeStandard.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidCodeStandard.svg)
Expand All @@ -276,12 +288,10 @@ new Toast

#### Android 技术 Q 群:10047167

#### 如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:
#### 如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:[点击查看捐赠列表](https://github.com/getActivity/Donate)

![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/pay_ali.png) ![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/pay_wechat.png)

#### [点击查看捐赠列表](https://github.com/getActivity/Donate)

#### 广告区

* 我现在任腾讯云服务器推广大使,大家如果有购买服务器的需求,可以通过下面的链接购买
Expand Down
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.toast.demo"
minSdkVersion 16
targetSdkVersion 33
versionCode 1203
versionName "12.3"
versionCode 1205
versionName "12.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -62,14 +62,14 @@ dependencies {
implementation 'com.google.android.material:material:1.4.0'

// 标题栏框架:https://github.com/getActivity/TitleBar
implementation 'com.github.getActivity:TitleBar:10.3'
implementation 'com.github.getActivity:TitleBar:10.5'

// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.github.getActivity:XXPermissions:18.0'
implementation 'com.github.getActivity:XXPermissions:18.3'

// 悬浮窗框架:https://github.com/getActivity/EasyWindow
implementation 'com.github.getActivity:EasyWindow:10.0'
implementation 'com.github.getActivity:EasyWindow:10.3'

// 内存泄漏捕捉:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.12'
}
9 changes: 7 additions & 2 deletions app/src/main/java/com/hjq/toast/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
Expand Down Expand Up @@ -59,6 +57,13 @@ public void showLongToast(View v) {
Toaster.showLong(R.string.demo_show_long_toast_result);
}

public void showCrossPageToast(View v) {
ToastParams params = new ToastParams();
params.text = getString(R.string.demo_show_cross_page_toast_result);
params.crossPageShow = true;
Toaster.show(params);
}

public void delayShowToast(View v) {
Toaster.delayedShow(R.string.demo_show_toast_with_two_second_delay_result, 2000);
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
android:onClick="showLongToast"
android:text="@string/demo_show_long_toast" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:onClick="showCrossPageToast"
android:text="@string/demo_show_cross_page_toast" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<string name="demo_show_toast">简单显示一个 Toast</string>
<string name="demo_show_short_toast">显示一个短 Toast</string>
<string name="demo_show_long_toast">显示一个长 Toast</string>
<string name="demo_show_cross_page_toast">显示一个跨页面的 Toast</string>
<string name="demo_show_toast_with_two_second_delay">延迟 2 秒显示 Toast</string>
<string name="demo_show_toast_in_the_subthread">在子线程中显示吐司</string>
<string name="demo_switch_to_white_style">切换为白色样式(局部生效)</string>
Expand All @@ -21,6 +22,7 @@
<string name="demo_show_toast_result">我是普通的 Toast我是普通的 Toast我是普通的 Toast我是普通的 Toast我是普通的 Toast</string>
<string name="demo_show_short_toast_result">我是一个短 Toast</string>
<string name="demo_show_long_toast_result">我是一个长 Toast</string>
<string name="demo_show_cross_page_toast_result">我是跨页面显示的 Toast</string>
<string name="demo_show_toast_with_two_second_delay_result">我是延迟 2 秒显示的 Toast</string>
<string name="demo_show_toast_in_the_subthread_result">我是子线程中弹出的吐司</string>
<string name="demo_switch_to_white_style_result">我是白色样式的 Toast</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<string name="demo_show_toast">Simple show a toast</string>
<string name="demo_show_short_toast">Show a short toast</string>
<string name="demo_show_long_toast">Show a long toast</string>
<string name="demo_show_cross_page_toast">Show a cross page toast</string>
<string name="demo_show_toast_with_two_second_delay">Show toast with 2 second delay</string>
<string name="demo_show_toast_in_the_subthread">Show toast in the subthread</string>
<string name="demo_switch_to_white_style">Switch to white style (local effect)</string>
Expand All @@ -22,6 +23,7 @@
<string name="demo_show_toast_result">I am an normal toast</string>
<string name="demo_show_short_toast_result">I am a short toast</string>
<string name="demo_show_long_toast_result">I am a long toast</string>
<string name="demo_show_cross_page_toast_result">I am a cross page toast</string>
<string name="demo_show_toast_with_two_second_delay_result">I am a toast displayed with a delay of 2 seconds</string>
<string name="demo_show_toast_in_the_subthread_result">I am the toast that pops up in the subthread</string>
<string name="demo_switch_to_white_style_result">I am toast in white style</string>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ allprojects {
setBuildDir(new File(rootDir, "build/${path.replaceAll(':', '/')}"))
}

task clean(type: Delete) {
tasks.register('clean', Delete) {
delete rootProject.buildDir
}
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 1203
versionName "12.3"
versionCode 1205
versionName "12.5"
}

// 使用 JDK 1.8
Expand Down
3 changes: 3 additions & 0 deletions library/src/main/java/com/hjq/toast/ToastParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class ToastParams {
/** 延迟显示时间 */
public long delayMillis = 0;

/** 是否跨页面展示(如果为 true 则优先用系统 Toast 实现) */
public boolean crossPageShow;

/** Toast 样式 */
public IToastStyle<?> style;

Expand Down
Loading

0 comments on commit 5804d7c

Please sign in to comment.