Skip to content

Commit

Permalink
6.9.6 update
Browse files Browse the repository at this point in the history
- 支持 DataBinding 的 BaseActivity 现在独立为 BaseBindingActivity,避免使用非 DataBinding 的用户产生异常;
- 废弃 BaseFrameworkSettings.useDataBinding,现在只需要继承对应的 Binding 基础件即可使用 DataBinding 模式;
  • Loading branch information
kongzue committed Feb 24, 2024
1 parent f2ba092 commit 0067fe8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.kongzue.baseframeworkdemo"
minSdkVersion 15
targetSdkVersion 30
versionCode 134
versionName "6.9.5"
versionCode 135
versionName "6.9.6"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Point;
import android.net.Uri;
Expand All @@ -38,7 +37,6 @@

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
Expand Down Expand Up @@ -85,8 +83,6 @@
import com.kongzue.baseframework.util.swipeback.util.SwipeBackUtil;
import com.kongzue.baseframework.util.toast.Toaster;

import androidx.viewbinding.ViewBinding;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand All @@ -105,7 +101,7 @@
* @link: http://kongzue.com/
* @describe: 自动化代码流水线作业,以及对原生安卓、MIUI、flyme的透明状态栏显示灰色图标文字的支持,同时提供一些小工具简化开发难度,详细说明文档:https://github.com/kongzue/BaseFramework
*/
public abstract class BaseActivity<VB extends ViewBinding> extends AppCompatActivity implements SwipeBackActivityBase {
public abstract class BaseActivity extends AppCompatActivity implements SwipeBackActivityBase {

private LifeCircleListener lifeCircleListener; //快速管理生命周期
private static GlobalLifeCircleListener globalLifeCircleListener; //全局生命周期
Expand Down Expand Up @@ -136,8 +132,6 @@ public abstract class BaseActivity<VB extends ViewBinding> extends AppCompatActi
private Bundle savedInstanceState;
private SwipeBackActivityHelper mHelper;

protected VB binding;

@Override
@Deprecated
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -169,17 +163,7 @@ protected void onCreate(Bundle savedInstanceState) {
if (layoutResId == -1) {
View contentView = resetContentView();
if (contentView == null) {
if (BaseFrameworkSettings.useDataBinding) {
View rootView = userDataBindingCreateLayout();
if (rootView != null) {
setContentView(rootView);
} else {
guessLayoutResToSetContentView();
}
} else {
guessLayoutResToSetContentView();
}

guessLayoutResToSetContentView();
} else {
setContentView(resetContentView());
}
Expand Down Expand Up @@ -232,26 +216,6 @@ private void guessLayoutResToSetContentView() {
}
}

private View userDataBindingCreateLayout() {
String className = getClass().getSimpleName();
if (className.endsWith("Activity")) {
className = className.substring(0, className.length() - 8);
}
String bindingClassName = getPackageName() + ".databinding.Activity" + className + "Binding";

try {
// 通过反射实例化Binding对象
Class<?> bindingClass = Class.forName(bindingClassName);
Method inflateMethod = bindingClass.getMethod("inflate", LayoutInflater.class);
binding = (VB) inflateMethod.invoke(null, getLayoutInflater());

return binding.getRoot();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

private String guessNameOfLayoutResId() {
String[] words = getClass().getSimpleName().split("(?<!^)(?=[A-Z])");
StringBuffer stringBuffer = new StringBuffer(words.length);
Expand Down Expand Up @@ -517,7 +481,7 @@ public void finishActivity() {
/**
* initViews会在启动时首先执行,建议在此方法内进行布局绑定、View初始化等操作
*/
public void initViews(){};
public abstract void initViews();

/**
* initDatas会在布局加载后执行,建议在此方法内加载数据和处理布局显示数据
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.kongzue.baseframework;

import android.view.LayoutInflater;
import android.view.View;

import androidx.viewbinding.ViewBinding;

import com.kongzue.baseframework.util.JumpParameter;

import java.lang.reflect.Method;

public abstract class BaseBindingActivity<VB extends ViewBinding> extends BaseActivity{

protected VB binding;

@Override
public void initViews() {

}

@Override
public abstract void initDatas(JumpParameter parameter);

@Override
public abstract void setEvents();

@Override
public View resetContentView() {
return userDataBindingCreateLayout();
}

private View userDataBindingCreateLayout() {
String className = getClass().getSimpleName();
if (className.endsWith("Activity")) {
className = className.substring(0, className.length() - 8);
}
String bindingClassName = getPackageName() + ".databinding.Activity" + className + "Binding";

try {
// 通过反射实例化Binding对象
Class<?> bindingClass = Class.forName(bindingClassName);
Method inflateMethod = bindingClass.getMethod("inflate", LayoutInflater.class);
binding = (VB) inflateMethod.invoke(null, getLayoutInflater());

return binding.getRoot();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,6 @@ private static String createDeviceId() {
public static int defaultActivityExitOutAnimRes = 0;

//使用DataBinding
@Deprecated
public static boolean useDataBinding = false;
}

0 comments on commit 0067fe8

Please sign in to comment.