前言
原文地址:http://blog.csdn.net/caption_deng/article/details/52870222
Intent在我們開發(fā)中一直在使用,但是我們在沒有寫在Base里面的時候每一次跳轉(zhuǎn)都會寫很多Intent mintent=new Intent();startActivity(mintent);這樣的方法脑漫,甚至有時候一個Activity(fragment)中會出現(xiàn)很多并且會傳值等祥绞,但是這樣會煩讼呢、代碼重復(fù)鲤竹,我結(jié)合網(wǎng)絡(luò)上一些有經(jīng)驗的前輩寫了一個intent工具
package com.util;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
/**
* Intent跳轉(zhuǎn)封裝
* Created by Administrator on 2018/1/16 0016.
*/
public class IntentUtil {
public static final String OPEN_ACTIVITY_KEY = "open_activity";//intent跳轉(zhuǎn)傳遞傳輸key
private static IntentUtil manager;
private static Intent intent;
private IntentUtil() {
}
public static IntentUtil get() {
if (manager == null) {
synchronized (IntentUtil.class) {
if (manager == null) {
manager = new IntentUtil();
}
}
}
intent = new Intent();
return manager;
}
/**
* 獲取上一個界面?zhèn)鬟f過來的參數(shù)
*
* @param activity this
* @param <T> 泛型
* @return
*/
public <T> T getParcelableExtra(Activity activity) {
Parcelable parcelable = activity.getIntent().getParcelableExtra(OPEN_ACTIVITY_KEY);
activity = null;
return (T) parcelable;
}
/**
* 啟動一個Activity
*
* @param _this
* @param _class
*/
public void goActivity(Context _this, Class<? extends Activity> _class) {
intent.setClass(_this, _class);
_this.startActivity(intent);
_this = null;
}
/**
* 啟動activity后kill前一個
*
* @param _this
* @param _class
*/
public void goActivityKill(Context _this, Class<? extends Activity> _class) {
intent.setClass(_this, _class);
_this.startActivity(intent);
((Activity) _this).finish();
_this = null;
}
/**
* 回調(diào)跳轉(zhuǎn)
*
* @param _this
* @param _class
* @param requestCode
*/
public void goActivityResult(Activity _this, Class<? extends Activity> _class, int requestCode) {
intent.setClass(_this, _class);
_this.startActivityForResult(intent, requestCode);
_this = null;
}
/**
* 回調(diào)跳轉(zhuǎn)并finish當前頁面
*
* @param _this
* @param _class
* @param requestCode
*/
public void goActivityResultKill(Activity _this, Class<? extends Activity> _class, int requestCode) {
intent.setClass(_this, _class);
_this.startActivityForResult(intent, requestCode);
((Activity) _this).finish();
_this = null;
}
/**
* 傳遞一個序列化實體類
*
* @param _this
* @param _class
* @param parcelable
*/
public void goActivity(Context _this, Class<? extends Activity> _class, Parcelable parcelable) {
intent.setClass(_this, _class);
putParcelable(parcelable);
_this.startActivity(intent);
_this = null;
}
/**
* 啟動一個Activity
*
* @param _this
* @param _class
* @param flags
* @param parcelable 傳遞的實體類
*/
public void goActivity(Context _this, Class<? extends Activity> _class, int flags, Parcelable parcelable) {
intent.setClass(_this, _class);
setFlags(flags);
putParcelable(parcelable);
_this.startActivity(intent);
_this = null;
}
public void goActivityKill(Context _this, Class<? extends Activity> _class, Parcelable parcelable) {
intent.setClass(_this, _class);
putParcelable(parcelable);
_this.startActivity(intent);
((Activity) _this).finish();
_this = null;
}
public void goActivityKill(Context _this, Class<? extends Activity> _class, int flags, Parcelable parcelable) {
intent.setClass(_this, _class);
setFlags(flags);
putParcelable(parcelable);
_this.startActivity(intent);
((Activity) _this).finish();
_this = null;
}
public void goActivityResult(Activity _this, Class<? extends Activity> _class, Parcelable parcelable, int requestCode) {
intent.setClass(_this, _class);
putParcelable(parcelable);
_this.startActivityForResult(intent, requestCode);
_this = null;
}
public void goActivityResult(Activity _this, Class<? extends Activity> _class, int flags, Parcelable parcelable, int requestCode) {
intent.setClass(_this, _class);
putParcelable(parcelable);
setFlags(flags);
_this.startActivityForResult(intent, requestCode);
_this = null;
}
public void goActivityResultKill(Activity _this, Class<? extends Activity> _class, Parcelable parcelable, int requestCode) {
intent.setClass(_this, _class);
putParcelable(parcelable);
_this.startActivityForResult(intent, requestCode);
_this.finish();
_this = null;
}
public void goActivityResultKill(Activity _this, Class<? extends Activity> _class, int flags, Parcelable parcelable, int requestCode) {
intent.setClass(_this, _class);
putParcelable(parcelable);
setFlags(flags);
_this.startActivityForResult(intent, requestCode);
_this.finish();
_this = null;
}
private void setFlags(int flags) {
if (flags < 0) return;
intent.setFlags(flags);
}
private void putParcelable(Parcelable parcelable) {
if (parcelable == null) return;
intent.putExtra(OPEN_ACTIVITY_KEY, parcelable);
}
}
//使用方法
IntentUtil.get().goActivity(this, MainActivity.class);