大家好,又见面了,我是你们的朋友全栈君。
Activity 跳转页面工具类
package com.kun.arno.test.tools;
import android.content.Intent;
import android.support.v4.app.Fragment;
import java.io.Serializable;
/**
* @author Arno
* @date 2018/7/18 0018
* @des
*/
public final class ActivityJumpUtils {
private static ActivityJumpUtils activityJumpUtils = new ActivityJumpUtils();
private ActivityJumpUtils() {
}
/***
* 获得AppManager的实例
*
* @return AppManager实例
*/
public static ActivityJumpUtils getInstance() {
if (activityJumpUtils == null) {
activityJumpUtils = new ActivityJumpUtils();
}
return activityJumpUtils;
}
/**
* 界面跳转
*
* @param clazz 目标Activity
*/
public void jumpTo(Class<?> clazz) {
jumpTo(clazz, null);
}
/**
* 跳转界面, 传参
*
* @param clazz 目标Activity
* @param data 传递{@code key} jumpTo {@code value} Serializable序列化的数据
*/
public <T extends Serializable> void jumpTo(Class<?> clazz, T data) {
jumpTo(clazz, "jumpTo", data);
}
/**
* 跳转界面, 传参
*
* @param clazz 目标Activity
* @param key 传递数据的key
* @param data 传递Serializable序列化的数据
*/
public <T extends Serializable> void jumpTo(Class<?> clazz, String key, T data) {
Intent intent = new Intent(ActivityManager.getInstance().currentActivity(), clazz);
if (null != data) {
intent.putExtra(key, data);
}
ActivityManager.getInstance().currentActivity().startActivity(intent);
}
/**
* startActivityForResult
*
* @param clazz 目标Activity
* @param requestCode 发送判断值
*/
public void jumpToForResult(Class<?> clazz, int requestCode) {
jumpToForResult(clazz, requestCode, null);
}
/**
* startActivityForResult with bundle
*
* @param clazz 目标Activity
* @param requestCode 发送判断值
* @param data 数据
*/
public <T extends Serializable> void jumpToForResult(Class<?> clazz, int requestCode, T data) {
jumpToForResult(clazz, requestCode, "jumpToForResult", data);
}
/**
* startActivityForResult with bundle
*
* @param clazz 目标Activity
* @param requestCode 发送判断值
* @param data 数据
*/
public <T extends Serializable> void jumpToForResult(Class<?> clazz, int requestCode, String key, T data) {
Intent intent = new Intent(ActivityManager.getInstance().currentActivity(), clazz);
if (null != data) {
intent.putExtra(key, data);
}
ActivityManager.getInstance().currentActivity().startActivityForResult(intent, requestCode);
}
/**
* startActivityForResult( fragment要返回值的方法)
*
* @param clazz 目标Activity
* @param requestCode 发送判断值
*/
public <T extends Serializable> void jumpToForResult(Fragment fragment, Class<?> clazz, int requestCode) {
jumpToForResult(fragment, clazz, requestCode, null);
}
/**
* startActivityForResult with bundle ( fragment要返回值的方法)
*
* @param clazz 目标fragment
* @param requestCode 发送判断值
* @param data 数据
*/
public <T extends Serializable> void jumpToForResult(Fragment fragment, Class<?> clazz, int requestCode, T data) {
jumpToForResult(fragment, clazz, requestCode, "jumpToForResult", data);
}
/**
* startActivityForResult with bundle ( fragment要返回值的方法)
*
* @param clazz 目标fragment
* @param requestCode 发送判断值
* @param data 数据
*/
public <T extends Serializable> void jumpToForResult(Fragment fragment, Class<?> clazz, int requestCode, String key, T data) {
Intent intent = new Intent(ActivityManager.getInstance().currentActivity(), clazz);
if (null != data) {
intent.putExtra(key, data);
}
fragment.startActivityForResult(intent, requestCode);
}
}
Activity 页面跳转
ActivityJumpUtils.getInstance().jumpTo(TestActivity.class);
Activity 页面跳转携带序列化参数
ActivityJumpUtils.getInstance().jumpTo(TestActivity.class,"key","value");
Activity页面回调
ActivityJumpUtils.getInstance().jumpToForResult(TestActivity.class,0x11);
Activity页面回调携带序列化参数
ActivityJumpUtils.getInstance().jumpToForResult(TestActivity.class,0x11,"key","value");
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/143344.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...