Android内部线程集中管理,不能直接new Handler new WebView?
-- Base基类等封装- https://github.com/jenly1314/Base/tree/master/src/main/java/com/king/base
Android 自定义Activity基类- http://blog.csdn.net/u013045971/article/details/47418093
Fragment懒加载实现,BaseFragment封装- https://www.jianshu.com/p/4eb74c9d5766
把更多的公用性的东西写成可配置化的?
> Android BaseActivity封装
1.BaseParentActivity抽象类封装
public abstract class BaseParentActivity extends RxAppCompatActivity implements BaseViewInterface {
protected LayoutInflater mInflater;
protected Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppManager.getAppManager().addActivity(this);
if (getLayoutId() != 0) {
setContentView(getLayoutId());
}
mInflater = getLayoutInflater();
mContext = this;
initTitleBar();
initView();
initData();
}
/**
* 打开一个Activity 默认 不关闭当前activity
* @param clz
*/
public void gotoActivity(Class<?> clz) {
gotoActivity(clz, false, null);
}
public void gotoActivity(Class<?> clz,boolean isCloseCurrentActivity) {
gotoActivity(clz, isCloseCurrentActivity, null);
}
public void gotoActivity(Class<?> clz,boolean isCloseCurrentActivity,Bundle ex) {
Intent intent=new Intent(this, clz);
if(ex!=null)
intent.putExtras(ex);
startActivity(intent);
if (isCloseCurrentActivity) {
finish();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
protected int getLayoutId() {
return 0;
}
protected void initTitleBar() {
}
}
2. BaseActivity抽象类进一步封装
public abstract class BaseActivity extends BaseParentActivity implements View.OnClickListener{
@BindView(R.id.title_text)
protected TextView mTitleText;
@BindView(R.id.back_img)
protected ImageView mBackBtn;
@BindView(R.id.right_img)
protected ImageView mRightBtn;
@Override
protected void initTitleBar() {
ButterKnife.bind(this);
}
@Override
@OnClick({R.id.back_img})
public void onClick(View view) {
switch (view.getId()) {
case R.id.back_img:
onBackPressed();
break;
default:
break;
}
}
}
3. CommentActivity实现
public class CommentActivity extends BaseActivity {
@Override
protected int getLayoutId() {
return R.layout.activity_comment;
}
@Override
public void initView() {
mTitleText.setText(R.string.comment);
}
@Override
public void initData() {
}
}
-- WebView单例封装 Android ?
-- 全局Handler的封装- https://blog.csdn.net/Crazyencode/article/details/79997907
public class GlobalHandler extends Handler{
private HandleMsgListener listener;
private String Tag = GlobalHandler.class.getSimpleName();
//使用单例模式创建GlobalHandler
private GlobalHandler(){
Log.e(Tag,"GlobalHandler创建");
}
private static class Holder{
private static final GlobalHandler HANDLER = new GlobalHandler();
}
public static GlobalHandler getInstance(){
return Holder.HANDLER;
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (getHandleMsgListener() != null){
getHandleMsgListener().handleMsg(msg);
}else {
Log.e(Tag,"请传入HandleMsgListener对象");
}
}
public interface HandleMsgListener{
void handleMsg(Message msg);
}
public void setHandleMsgListener(HandleMsgListener listener){
this.listener = listener;
}
public HandleMsgListener getHandleMsgListener(){
return listener;
}
}
public class MainActivity extends AppCompatActivity implements GlobalHandler.HandleMsgListener{
private GlobalHandler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler = GlobalHandler.getInstance();
mHandler.setHandleMsgListener(this);
mHandler.sendEmptyMessage(0);
mHandler.sendEmptyMessage(1);
}
@Override
public void handleMsg(Message msg) {
switch (msg.what){
case 0:
Toast.makeText(MainActivity.this,"test0",Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this,"test1",Toast.LENGTH_SHORT).show();
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//销毁移除所有消息,避免内存泄露
mHandler.removeCallbacks(null);
}
}