A while ago, I wanted to make a pop-up window effect for the APP when it entered the background. I used to monitor the life cycle of the base class base. The process was a bit unsatisfactory, so today I wrote a separate dependency on switching the APP to the background and switching to the foreground ( AppFrontBackHelper), so that if you use it in the future, you can directly quote it. In addition, it also adds the function of double-clicking to exit the APP and get and create all APPs.
Let's take a look at the effect first:
Instructions:
allprojects {
repositories {
...
maven {
url 'https://jitpack.io' }
}
}
implementation 'com.github.ZhaoChanghu:AppFrontBackHelper:1.1.4'
To build an application to register, don't forget to assign values in the manifest file, otherwise it will have no effect
Open the background pop-up window on the startup page
When double-clicking to exit the APP, it needs to be called in the return event
At this point, your APP already has the function of double-clicking to exit the APP, and the function of pop-up window when entering the background
.
.
.
.
.
Next, let's introduce the functions in detail
When the Application is registered, perform front and back statement assignment and other operations, allowing custom methods
1. Registration and statement statement
new AppFrontBackHelper()
.setToastTime(500) //设置弹窗时间,不设置默认 short
.setStopToast("APP已进入后台运行") //进入后台语句,需要在启动页打开
.setStartToast("APP已经进入前台") //进入前台语句,默认设置后就自动打开
.register();
2. Customize the method of entering the front and back, and you can customize the operation through this method
AppFrontBackHelper helper = new AppFrontBackHelper();
helper.register(new AppFrontBackHelper.OnAppStatusListener() {
@Override
public void onFront() {
//应用切到前台处理
Toast.makeText(getApplicationContext(),"我进入到前台了",Toast.LENGTH_SHORT).show();
}
@Override
public void onBack() {
//应用切到后台处理
Toast.makeText(getApplicationContext(),"我进入到后台了",Toast.LENGTH_SHORT).show();
}
});
3. Close the pop-up window to enter the background, which can be closed in any scope of dependence, and it is closed by default when double-clicking to exit
AppFrontBackHelper.stopToast();
4. Open the pop-up window to enter the background. By default, there is no pop-up window. It is recommended to open it on the startup page
AppFrontBackHelper.startToast();
5. Double-click to exit the APP, the first parameter is the pop-up window time, and the second parameter is the content
AppFrontBackHelper.setAPPFinish(2000,"再按一次退出应用");
6. Get all the created activities of the current application
AppFrontBackHelper.getALLActivity();
The above is all the usages of AppFrontBackHelper. The source code is very simple, just a Class, which can be reused directly after importing.