copy All Activity Fields via reflection - Android java.lang.reflect

Android examples for java.lang.reflect:Field

Description

copy All Activity Fields via reflection

Demo Code


//package com.java2s;
import android.app.Activity;

import java.lang.reflect.Field;

public class Main {
    public static boolean copyAllActivityFields(Activity src,
            Activity target) {//from  w w  w  .jav  a  2s .  co  m
        if (src == null || target == null) {
            return false;
        }

        Class<?> clsType = Activity.class;
        while (clsType != null) {
            Field[] fields = clsType.getDeclaredFields();
            for (Field fid : fields) {
                try {
                    fid.setAccessible(true);
                    Object ref = fid.get(src);
                    fid.set(target, ref);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            clsType = clsType.getSuperclass();
        }

        return true;
    }
}

Related Tutorials