lmw
2023-03-11 4df5bb59e5fe9f9d140e5610f7772dd8a05a28d4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package cn.sinata.xldutils.utils;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
 
import androidx.fragment.app.Fragment;
 
import java.io.Serializable;
import java.util.ArrayList;
 
/**
 *
 * Created by liaoxiang on 16/8/8.
 */
public class ActivityUtil {
    private Context fromActivity;
    private Fragment fragment;
    private Class goActivity;
    private Bundle bundle;
 
    private ActivityUtil(Context context){
        fromActivity = context;
//        this.goActivity = goActivity;
        bundle = new Bundle();
    }
    private ActivityUtil(Fragment fragment){
        this.fragment = fragment;
//        this.goActivity = goActivity;
        bundle = new Bundle();
    }
 
    public static ActivityUtil create(Context context){
        return new ActivityUtil(context);
    }
    public static ActivityUtil create(Fragment fragment){
        return new ActivityUtil(fragment);
    }
 
    public ActivityUtil go(Class goActivity){
        this.goActivity = goActivity;
        return this;
    }
    /**
     * 传递int参数
     * @param key 键
     * @param intValue 值
     * @return this
     */
    public ActivityUtil put(String key, int intValue){
        bundle.putInt(key,intValue);
        return this;
    }
    /**
     * 传递Parcelable参数
     * @param key 键
     * @param p 值
     * @return this
     */
    public ActivityUtil put(String key, Parcelable p){
        bundle.putParcelable(key,p);
        return this;
    }
 
    /**
     * 传递String参数
     * @param key 键
     * @param s 值
     * @return this
     */
    public ActivityUtil put(String key, String s){
        bundle.putString(key,s);
        return this;
    }
    /**
     * 传递float参数
     * @param key 键
     * @param f 值
     * @return this
     */
    public ActivityUtil put(String key, float f){
        bundle.putFloat(key,f);
        return this;
    }
    /**
     * 传递double参数
     * @param key 键
     * @param d 值
     * @return this
     */
    public ActivityUtil put(String key, double d){
        bundle.putDouble(key,d);
        return this;
    }
    /**
     * 传递Serializable参数
     * @param key 键
     * @param s 值
     * @return this
     */
    public ActivityUtil put(String key, Serializable s){
        bundle.putSerializable(key,s);
        return this;
    }
    /**
     * 传递StringList参数
     * @param key 键
     * @param a 值
     * @return this
     */
    public ActivityUtil putStringList(String key, ArrayList<String> a){
        bundle.putStringArrayList(key,a);
        return this;
    }
    /**
     * 传递ParcelableList参数
     * @param key 键
     * @param a 值
     * @return this
     */
    public ActivityUtil putParcelableList(String key, ArrayList<? extends Parcelable> a){
        bundle.putParcelableArrayList(key,a);
        return this;
    }
 
    /**
     * 跳转activity
     */
    public void start(){
        if (goActivity==null){
            return;
        }
        if (fromActivity!=null) {
            Intent intent = new Intent(fromActivity, goActivity);
            if (bundle != null && !bundle.isEmpty()) {
                intent.putExtras(bundle);
            }
            fromActivity.startActivity(intent);
        }else if (fragment!=null){
            Intent intent = new Intent(fragment.getContext(), goActivity);
            if (bundle != null && !bundle.isEmpty()) {
                intent.putExtras(bundle);
            }
            fragment.startActivity(intent);
        }
    }
 
    /**
     * 跳转activity 带requestCode
     * @param requestCode 请求code
     */
    public void startForResult(int requestCode){
        if (goActivity==null){
            return;
        }
        if (fromActivity!=null){
            Intent intent = new Intent(fromActivity,goActivity);
            if (bundle!=null && !bundle.isEmpty()){
                intent.putExtras(bundle);
            }
            if (fromActivity instanceof Activity) {
                ((Activity) fromActivity).startActivityForResult(intent, requestCode);
            }
        }else if (fragment!=null && !fragment.isDetached()){
            Intent intent = new Intent(fragment.getContext(),goActivity);
            if (bundle!=null && !bundle.isEmpty()){
                intent.putExtras(bundle);
            }
            fragment.startActivityForResult(intent, requestCode);
        }
    }
 
}