【一般的界面跳转方式如下】
Intent intent = new Intent();
intent.setClass(HelloworldActivity.this, NextActivity.class);// 这里强引用了NextActivity
HelloworldActivity.this.startActivity(intent);
【解耦的做法,利用反射的方法】
import android.app.Activity;
import android.content.Intent;
/***
* - 页面跳转器,让页面之间解耦合
* */
public class Navigator {
public static void navigatorTo(Activity from, final String toActivityName, final Intent intent){
if(from == null || toActivityName == null || intent == null){
throw new NullPointerException("from == null || toActivityName == null || intent == null");
}
Class<?> clazz = null;
try {
clazz = Class.forName(toActivityName);
if(clazz != null){
intent.setClass(from, clazz);