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 com.vst.functionframework.ui; import android.app.Activity; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.vst.functionframework.utils.VLoger; /** * @author kymjs (http://www.kymjs.com/) on 11/19/15. */ public abstract class SupportActivity extends AppCompatActivity implements View.OnClickListener, I_VstActivity, I_SkipActivity { public static final int WHICH_MSG = 0X37210; public Activity aty; protected VstFragment currentKJFragment; protected SupportFragment currentSupportFragment; /** * Activity? */ public int activityState = DESTROY; @Override public void initData() { } @Override public void 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; } /*************************************************************************** * print Activity callback methods ***************************************************************************/ @Override protected void onCreate(Bundle savedInstanceState) { aty = this; VstActivityStack.create().addActivity(this); VLoger.state(this.getClass().getName(), "---------onCreat "); super.onCreate(savedInstanceState); setRootView(); // annotate? AnnotateUtil.initBindView(this); initData(); initWidget(); } @Override protected void onStart() { super.onStart(); VLoger.state(this.getClass().getName(), "---------onStart "); } @Override protected void onResume() { super.onResume(); activityState = RESUME; VLoger.state(this.getClass().getName(), "---------onResume "); } @Override protected void onPause() { super.onPause(); activityState = PAUSE; VLoger.state(this.getClass().getName(), "---------onPause "); } @Override protected void onStop() { super.onStop(); activityState = STOP; VLoger.state(this.getClass().getName(), "---------onStop "); } @Override protected void onRestart() { super.onRestart(); VLoger.state(this.getClass().getName(), "---------onRestart "); } @Override protected void onDestroy() { activityState = DESTROY; VLoger.state(this.getClass().getName(), "---------onDestroy "); super.onDestroy(); VstActivityStack.create().finishActivity(this); currentKJFragment = null; currentSupportFragment = null; aty = null; } /** * 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, VstFragment 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(); } }