Activity 跳转页面工具类

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  void jumpTo(Class clazz, T data) {
        jumpTo(clazz, "jumpTo", data);
    }

    /**
     * 跳转界面,  传参
     *
     * @param clazz 目标Activity
     * @param key   传递数据的key
     * @param data  传递Serializable序列化的数据
     */
    public  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  void jumpToForResult(Class clazz, int requestCode, T data) {
        jumpToForResult(clazz, requestCode, "jumpToForResult", data);
    }

    /**
     * startActivityForResult with bundle
     *
     * @param clazz       目标Activity
     * @param requestCode 发送判断值
     * @param data        数据
     */
    public  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  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  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  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");

 

你可能感兴趣的:(Tools)