Java tutorial
/* * Copyright (c) 2014, . * * 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.kymjs.kjframe; import android.app.Activity; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.support.v4.app.FragmentActivity; import android.view.View; import org.kymjs.kjframe.ui.AnnotateUtil; import org.kymjs.kjframe.ui.I_BroadcastReg; import org.kymjs.kjframe.ui.I_KJActivity; import org.kymjs.kjframe.ui.I_SkipActivity; import org.kymjs.kjframe.ui.KJActivityStack; import org.kymjs.kjframe.ui.KJFragment; import org.kymjs.kjframe.ui.SupportFragment; import org.kymjs.kjframe.utils.KJLoger; import java.lang.ref.SoftReference; /** * Activity's framework,the developer shouldn't extends it <br> * <b></b> 2014-3-1 <br> * <b>?</b> 2014-10-17<br> * * @author kymjs (https://github.com/kymjs) * @version 1.8 */ public abstract class KJActivity extends FragmentActivity implements View.OnClickListener, I_BroadcastReg, I_KJActivity, I_SkipActivity { public static final int WHICH_MSG = 0X37210; public Activity aty; protected KJFragment currentKJFragment; protected SupportFragment currentSupportFragment; private ThreadDataCallBack callback; private KJActivityHandle threadHandle = new KJActivityHandle(this); protected final Handler mMainLoopHandler = new Handler(Looper.getMainLooper()); /** * Activity? */ public int activityState = DESTROY; /** * ????? */ private interface ThreadDataCallBack { void onSuccess(); } private static class KJActivityHandle extends Handler { private final SoftReference<KJActivity> mOuterInstance; KJActivityHandle(KJActivity outer) { mOuterInstance = new SoftReference<>(outer); } // ????? @Override public void handleMessage(android.os.Message msg) { if (msg.what == WHICH_MSG && mOuterInstance.get() != null) { mOuterInstance.get().callback.onSuccess(); } } } /** * initDataFromThread()???? */ protected void threadDataInited() { } /** * ????UI? */ @Override public void initDataFromThread() { callback = new ThreadDataCallBack() { @Override public void onSuccess() { threadDataInited(); } }; } @Override public void initData() { } @Override public void initWidget() { } // ?? private void initializer() { new Thread(new Runnable() { @Override public void run() { initDataFromThread(); threadHandle.sendEmptyMessage(WHICH_MSG); } }).start(); initData(); initWidget(); } /** * listened widget's click method */ @Override public void widgetClick(View v) { } @Override public void onClick(View v) { widgetClick(v); } @SuppressWarnings("unchecked") protected <T extends View> T bindView(int id) { return (T) findViewById(id); } @SuppressWarnings("unchecked") protected <T extends View> T bindView(int id, boolean click) { T view = (T) findViewById(id); if (click) { view.setOnClickListener(this); } return view; } @Override public void registerBroadcast() { } @Override public void unRegisterBroadcast() { } /*************************************************************************** * print Activity callback methods ***************************************************************************/ @Override protected void onCreate(Bundle savedInstanceState) { aty = this; KJActivityStack.create().addActivity(this); KJLoger.state(this.getClass().getName(), "---------onCreat "); setRootView(); // annotate? AnnotateUtil.initBindView(this); initializer(); registerBroadcast(); super.onCreate(savedInstanceState); } @Override protected void onStart() { super.onStart(); KJLoger.state(this.getClass().getName(), "---------onStart "); } @Override protected void onResume() { super.onResume(); activityState = RESUME; KJLoger.state(this.getClass().getName(), "---------onResume "); } @Override protected void onPause() { super.onPause(); activityState = PAUSE; KJLoger.state(this.getClass().getName(), "---------onPause "); } @Override protected void onStop() { super.onStop(); activityState = STOP; KJLoger.state(this.getClass().getName(), "---------onStop "); } @Override protected void onRestart() { super.onRestart(); KJLoger.state(this.getClass().getName(), "---------onRestart "); } @Override protected void onDestroy() { unRegisterBroadcast(); activityState = DESTROY; KJLoger.state(this.getClass().getName(), "---------onDestroy "); super.onDestroy(); KJActivityStack.create().finishActivity(this); } /** * skip to @param(cls)and call @param(aty's) finish() method */ @Override public void skipActivity(Activity aty, Class<?> cls) { showActivity(aty, cls); aty.finish(); } /** * skip to @param(cls)and call @param(aty's) finish() method */ @Override public void skipActivity(Activity aty, Intent it) { showActivity(aty, it); aty.finish(); } /** * skip to @param(cls)and call @param(aty's) finish() method */ @Override public void skipActivity(Activity aty, Class<?> cls, Bundle extras) { showActivity(aty, cls, extras); aty.finish(); } /** * show to @param(cls)but can't finish activity */ @Override public void showActivity(Activity aty, Class<?> cls) { Intent intent = new Intent(); intent.setClass(aty, cls); aty.startActivity(intent); } /** * show to @param(cls)but can't finish activity */ @Override public void showActivity(Activity aty, Intent it) { aty.startActivity(it); } /** * show to @param(cls)but can't finish activity */ @Override public void showActivity(Activity aty, Class<?> cls, Bundle extras) { Intent intent = new Intent(); intent.putExtras(extras); intent.setClass(aty, cls); aty.startActivity(intent); } /** * Fragment? * * @param resView ?? * @param targetFragment ??Fragment */ public void changeFragment(int resView, KJFragment targetFragment) { if (targetFragment.equals(currentKJFragment)) { return; } FragmentTransaction transaction = getFragmentManager().beginTransaction(); if (!targetFragment.isAdded()) { transaction.add(resView, targetFragment, targetFragment.getClass().getName()); } if (targetFragment.isHidden()) { transaction.show(targetFragment); targetFragment.onChange(); } if (currentKJFragment != null && currentKJFragment.isVisible()) { transaction.hide(currentKJFragment); } currentKJFragment = targetFragment; transaction.commit(); } /** * Fragment? * * @param resView ?? * @param targetFragment ??Fragment */ public void changeFragment(int resView, SupportFragment targetFragment) { if (targetFragment.equals(currentSupportFragment)) { return; } android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if (!targetFragment.isAdded()) { transaction.add(resView, targetFragment, targetFragment.getClass().getName()); } if (targetFragment.isHidden()) { transaction.show(targetFragment); targetFragment.onChange(); } if (currentSupportFragment != null && currentSupportFragment.isVisible()) { transaction.hide(currentSupportFragment); } currentSupportFragment = targetFragment; transaction.commit(); } /** * Fragment? * * @param resView ?? * @param targetFragment ??Fragment * @param isBack ??true */ public void changeFragment(int resView, KJFragment targetFragment, boolean isBack) { if (targetFragment.equals(currentKJFragment)) { return; } FragmentTransaction transaction = getFragmentManager().beginTransaction(); if (!targetFragment.isAdded()) { transaction.add(resView, targetFragment, targetFragment.getClass().getName()); } if (targetFragment.isHidden()) { transaction.show(targetFragment); targetFragment.onChange(); } if (currentKJFragment != null && currentKJFragment.isVisible()) { transaction.hide(currentKJFragment); } if (isBack) { transaction.addToBackStack(null); } currentKJFragment = targetFragment; transaction.commit(); } /** * Fragment? * * @param resView ?? * @param targetFragment ??Fragment * @param isBack ??true */ public void changeFragment(int resView, SupportFragment targetFragment, boolean isBack) { if (targetFragment.equals(currentSupportFragment)) { return; } android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if (!targetFragment.isAdded()) { transaction.add(resView, targetFragment, targetFragment.getClass().getName()); } if (targetFragment.isHidden()) { transaction.show(targetFragment); targetFragment.onChange(); } if (currentSupportFragment != null && currentSupportFragment.isVisible()) { transaction.hide(currentSupportFragment); } if (isBack) { transaction.addToBackStack(null); } currentSupportFragment = targetFragment; transaction.commit(); } }