Java tutorial
/* * Copyright (C) 2016 Claymain Twinkle. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.kesar.lazy.primary.android.activity; import android.app.ProgressDialog; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import org.kesar.lazy.primary.utils.LoggerUtils; import butterknife.ButterKnife; import butterknife.Unbinder; /** * Activity * Created by kesar on 2016/5/6. */ public abstract class BaseActivity extends AppCompatActivity { private final String TAG = BaseActivity.class.getName(); private Unbinder mUnBinder; private Fragment mCurrentFragment; ProgressDialog mLoadingDialog; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { LoggerUtils.d("onCreate"); super.onCreate(savedInstanceState); beforeInitLayoutView(); setContentView(getLayoutId()); beforeBindView(); mUnBinder = ButterKnife.bind(this); afterBindView(); } @Override protected void onRestart() { LoggerUtils.d("onRestart"); super.onRestart(); } @Override protected void onStart() { LoggerUtils.d("onStart"); super.onStart(); } @Override protected void onResume() { LoggerUtils.d("onResume"); super.onResume(); } @Override protected void onPause() { LoggerUtils.d("onPause"); super.onPause(); } @Override protected void onStop() { LoggerUtils.d("onStop"); super.onStop(); } @Override protected void onDestroy() { LoggerUtils.d("onDestroy"); unBindView(); super.onDestroy(); } /** * ?layout view id * * @return layout view id */ protected abstract int getLayoutId(); /** * before setContentView */ protected void beforeInitLayoutView() { LoggerUtils.d("beforeInitLayoutView"); } /** * before buffer knife bind view */ protected void beforeBindView() { LoggerUtils.d("beforeBindView"); } /** * after buffer knife bind view */ protected void afterBindView() { LoggerUtils.d("afterBindView"); } /** * view */ private void unBindView() { if (mUnBinder != null) { mUnBinder.unbind(); } } /** * ?Fragment * * @param resView * @param targetFragment */ public void changeFragment(int resView, Fragment targetFragment) { if (targetFragment.equals(mCurrentFragment)) { return; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if (!targetFragment.isAdded()) { transaction.add(resView, targetFragment, targetFragment.getClass().getName()); } if (targetFragment.isHidden()) { transaction.show(targetFragment); } if (mCurrentFragment != null && mCurrentFragment.isVisible()) { transaction.hide(mCurrentFragment); } mCurrentFragment = targetFragment; transaction.commit(); } /** * ??Fragment * * @return */ public Fragment getCurrentFragment() { return mCurrentFragment; } /** * ? * * @param message ? */ public void showDialog(String message) { if (mLoadingDialog == null) { mLoadingDialog = new ProgressDialog(this); mLoadingDialog.setCanceledOnTouchOutside(false); } mLoadingDialog.setMessage(message); if (!mLoadingDialog.isShowing()) { this.runOnUiThread(new Runnable() { @Override public void run() { mLoadingDialog.show(); } }); } } /** * ??? */ public void dismissDialog() { if (mLoadingDialog != null && mLoadingDialog.isShowing()) { mLoadingDialog.dismiss(); } } }