feat: 处理页面间传递数据

This commit is contained in:
niushuai233 2022-10-28 15:34:05 +08:00
parent c37aecc354
commit 6eea9d57ad
6 changed files with 125 additions and 18 deletions

View File

@ -1,5 +1,7 @@
package cc.niushuai.project.devcontrol.base.ui;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
@ -15,6 +17,7 @@ import cc.niushuai.project.devcontrol.base.util.Global;
import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.base.util.ToastUtil;
import cc.niushuai.project.devcontrol.base.util.UiUtil;
import cc.niushuai.project.devcontrol.base.util.XLog;
import cc.niushuai.project.devcontrol.ui.common.IconSelectDialogFragment;
import cc.niushuai.project.devcontrol.vo.DeviceInfo;
import cn.hutool.core.util.StrUtil;
@ -98,17 +101,6 @@ public abstract class BaseActivity extends AppCompatActivity {
protected void addListener() {
}
/**
* 返回上一页
*
* @param activity
* @author niushuai
* @date: 2022/10/19 14:46
*/
protected void activityButtonBackClickListener(BaseActivity activity) {
findViewById(R.id.activity_title_back).setOnClickListener(view -> activity.finish());
}
/**
* 自定义back的点击事件
*
@ -120,6 +112,65 @@ public abstract class BaseActivity extends AppCompatActivity {
findViewById(R.id.activity_title_back).setOnClickListener(onClickListener);
}
/**
* 返回上一页
*
* @param activity
* @author niushuai
* @date: 2022/10/19 14:46
*/
protected void activityButtonBackClickListener(BaseActivity activity) {
findViewById(R.id.activity_title_back).setOnClickListener(view -> {
XLog.v(Keys.Tag.ACTIVITY_JUMP, "{}.activityButtonBackClickListener: 携带device数据", this.getClass().getSimpleName());
withBackData(activity);
activity.finish();
});
}
/**
* 返回上一层时携带当前的device信息
*
* @param activity
* @author niushuai233
* @date: 2022/10/28 14:59
*/
private void withBackData(BaseActivity activity) {
Intent data = new Intent();
data.putExtra(Keys.DEVICE_INFO, this.device);
activity.setResult(Keys.RequestCode.GENERAL, data);
}
/**
* 返回键被按下监控事件
*
* @author niushuai233
* @date: 2022/10/28 14:59
*/
@Override
public void onBackPressed() {
XLog.v(Keys.Tag.ACTIVITY_JUMP, "{}.onBackPressed: 携带device数据", this.getClass().getSimpleName());
withBackData(this);
super.onBackPressed();
}
/**
* 接收其他页面传输的数据
*
* @param requestCode
* @param resultCode
* @param data
* @author niushuai233
* @date: 2022/10/28 15:04
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
XLog.v(Keys.Tag.ACTIVITY_JUMP, "{}.onActivityResult: requestCode: {}, resultCode: {}, 处理返回数据: {}", this.getClass().getSimpleName(), requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
if (null!= data && null != data.getSerializableExtra(Keys.DEVICE_INFO)) {
this.device = (DeviceInfo) data.getSerializableExtra(Keys.DEVICE_INFO);
}
}
/**
* 更多设置 打开新的activity
*
@ -136,7 +187,7 @@ public abstract class BaseActivity extends AppCompatActivity {
moreSetView.setVisibility(View.GONE);
return;
}
moreSetView.setOnClickListener(view -> ActivityUtil.startActivity(activity, clazz, withData));
moreSetView.setOnClickListener(view -> ActivityUtil.startActivityForResult(activity, clazz, withData, Keys.RequestCode.GENERAL));
}
/**
@ -156,7 +207,7 @@ public abstract class BaseActivity extends AppCompatActivity {
moreSetView.setVisibility(View.GONE);
return;
}
moreSetView.setOnClickListener(view -> ActivityUtil.startActivity(activity, clazz, keys, values));
moreSetView.setOnClickListener(view -> ActivityUtil.startActivityForResult(activity, clazz, keys, values, Keys.RequestCode.GENERAL));
}
/**
@ -205,6 +256,30 @@ public abstract class BaseActivity extends AppCompatActivity {
Global.initDeviceInfoMap();
}
/**
* 返回上一页
*
* @author niushuai233
* @date: 2022/10/28 15:16
*/
protected void back() {
this.finish();
}
/**
* 携带deviceInfo返回上一页
*
* @author niushuai233
* @date: 2022/10/28 15:16
*/
protected void backWithDeviceInfo() {
Intent data = new Intent();
data.putExtra(Keys.DEVICE_INFO, this.device);
this.setResult(Keys.RequestCode.GENERAL, data);
XLog.v(Keys.Tag.ACTIVITY_JUMP, "{}.backWithDeviceInfo 携带device数据", this.getClass().getSimpleName());
this.back();
}
/**
* 执行命令 返回成功与否

View File

@ -25,8 +25,7 @@ public class ActivityUtil {
startActivity(sourceActivity, targetActivity, map);
}
public static void startActivity(Activity sourceActivity, Class<? extends BaseActivity> targetActivity,
Map<String, String> withData) {
public static void startActivity(Activity sourceActivity, Class<? extends BaseActivity> targetActivity, Map<String, String> withData) {
Intent intent = new Intent(sourceActivity, targetActivity);
if (null != withData) {
for (String key : withData.keySet()) {
@ -35,4 +34,24 @@ public class ActivityUtil {
}
sourceActivity.startActivity(intent);
}
public static void startActivityForResult(Activity sourceActivity, Class<? extends BaseActivity> targetActivity, Map<String, String> withData, Integer requestCode) {
Intent intent = new Intent(sourceActivity, targetActivity);
if (null != withData) {
for (String key : withData.keySet()) {
intent.putExtra(key, withData.get(key));
}
}
sourceActivity.startActivityForResult(intent, requestCode);
}
public static void startActivityForResult(Activity sourceActivity, Class<? extends BaseActivity> targetActivity, String[] keys, String[] values, Integer requestCode) {
HashMap<String, String> map = new HashMap<>();
for (int i = 0; i < keys.length; i++) {
map.put(keys[i], values[i]);
}
startActivityForResult(sourceActivity, targetActivity, map, requestCode);
}
}

View File

@ -47,7 +47,7 @@ public class ExecUtil {
XLog.i(Keys.Tag.EXEC_COMMAND, "执行命令: {}, 结果: {}", command, result);
return new CommandResult<>(true, result);
} catch (Exception e) {
XLog.e(Keys.Tag.EXEC_COMMAND, "执行命令: {}, 异常: {}", command, e.getMessage(), e);
XLog.e(Keys.Tag.EXEC_COMMAND, "执行命令: {}, 异常", command, e);
return new CommandResult<>(false, e.getMessage());
}
}

View File

@ -3,6 +3,8 @@ package cc.niushuai.project.devcontrol.base.util;
public interface Keys {
String ID = "id";
String DEVICE = "device";
String DEVICE_INFO = "deviceInfo";
String DB_FILE_NAME = "dev_control.db";
@ -22,5 +24,14 @@ public interface Keys {
String KEEP_DAY_SELECT = "KeepDaySelect";
String APP_SETUP = "AppSetup";
String EXEC_COMMAND = "ExecCommand";
String ACTIVITY_JUMP = "ActivityJump";
}
interface RequestCode {
int GENERAL = 1;
}
}

View File

@ -53,7 +53,7 @@ public class PowerSwitchSetActivity extends BaseActivity {
HashMap<String, String> withData = new HashMap<>(1);
withData.put(Keys.ID, device.getId());
ActivityUtil.startActivity(this, PowerSwitchSetParamActivity.class, withData);
ActivityUtil.startActivityForResult(this, PowerSwitchSetParamActivity.class, withData, Keys.RequestCode.GENERAL);
}
private void outsideLogViewClickListener(View view) {

View File

@ -1,5 +1,6 @@
package cc.niushuai.project.devcontrol.ui.powerswitch;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
@ -11,6 +12,7 @@ import cc.niushuai.project.devcontrol.R;
import cc.niushuai.project.devcontrol.base.ui.BaseActivity;
import cc.niushuai.project.devcontrol.base.util.CommonUiUtil;
import cc.niushuai.project.devcontrol.base.util.Global;
import cc.niushuai.project.devcontrol.base.util.Keys;
import cc.niushuai.project.devcontrol.databinding.ActivityPowerSwitchSetParamBinding;
import cc.niushuai.project.devcontrol.db.DB;
import cc.niushuai.project.devcontrol.vo.DeviceInfo;
@ -76,7 +78,7 @@ public class PowerSwitchSetParamActivity extends BaseActivity {
device = deviceInfo;
// 返回上一层
this.finish();
super.backWithDeviceInfo();
}
@Override