代理模式创建Activity
Posted 2015xbx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代理模式创建Activity相关的知识,希望对你有一定的参考价值。
代码实现采用构造方法传递参数传递Activity实例的方式
public class ActivityHandler { protected Activity mActivityInstance; public ActivityHandler(Activity activity) { mActivityInstance = activity; } // this one is called before super.onCreate public void onPreCreate(Bundle savedInstance) { } public void onCreate(Bundle savedInstance) { ……实现一些公共的创建逻辑 } public void onRestoreInstanceState(Bundle savedInstance) { } public View onCreateView(String name, Context context, AttributeSet attrs) { return null; } public void onNewIntent() { } public void onStart() { } public void onResume() { } public void onPostResume() { } public void onPause() { } public void onSaveInstanceState(Bundle outState) { } public void onStop() { } public void onDestroy() { } public void finish() { } public void onActivityResult(int requestCode, int resultCode, Intent data) { } public boolean onBackPressed() { return false; } public void onConfigurationChanged(Configuration newConfig) { } protected boolean isActivityFinished() { return mActivityInstance.isFinishing(); } protected void onFragmentCallback(int action, Bundle data) { } protected void setActionBar(final String title) { …… } protected void setBackIconVisibility(int visibility) { ……封装一些统一的Activity的方法 } public boolean onCreateOptionsMenu() { return false; } public boolean onOptionsHomeItemSelected() { return false; } public View getSupportActionBar() { …… } protected boolean inflateMenu(int menuLayoutId) { ……实现公共的菜单 } public void dispatchTouchEvent(MotionEvent ev) { } public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { } }
public abstract class BaseActivity extends ActivityHandler:
抽象类再封装一次,提供一些具体业务操作的公共方法,和回调的抽象方法
public class Test1Activity extends BaseActivity:
具体的Activity实现逻辑,重载ActivityHandler的方法,主要的代码逻辑是加载layout文件,处理layout的交互等
public class ProxyActivity extends Activity :
真正的Activity作为一个代理,所有的Activity创建都通过该代理类来进行创建。
static final Intent getLaunchIntent(Context ctx, String action, Bundle extras) {
return ProxyActivity.getLaunchIntent(ctx, action, extras, ForumSDKActivity.class);
}
static final Intent getLaunchIntent(Context ctx, String action, Bundle extras,
Class<? extends ProxyActivity > extend) {
Intent intent = new Intent(ctx, extend == null ? ProxyActivity.class : extend);
intent.setAction(action);
if (extras != null) {
intent.putExtras(extras);
}
return intent;
}
Intent intent = ProxyActivity .getLaunchIntent(mContext, ProxyActivity .ACTIONS.XXXXXX, null);
mContext.startActivity(intent);
好处就是封装和抽象 代码共享,Activity的统一入口
以上是关于代理模式创建Activity的主要内容,如果未能解决你的问题,请参考以下文章