org.jp.liteframe.LiteActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.jp.liteframe.LiteActivity.java

Source

/*
 * 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.jp.liteframe;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import org.jp.liteframe.ui.I_BroadcastReg;
import org.jp.liteframe.ui.I_LiteActivity;
import org.jp.liteframe.ui.I_SkipActivity;
import org.jp.liteframe.ui.LiteActivityStack;
import org.jp.liteframe.ui.LiteFragment;
import org.jp.liteframe.ui.SupportFragment;
import org.jp.liteframe.utils.Loger;

import java.lang.ref.SoftReference;

import butterknife.ButterKnife;

/**
 * Activity's framework,the developer shouldn't extends it <br>
 * <b></b> 2014-3-1 <br>
 * <b>?</b> 2014-10-17<br>
 *
 * @author kymjs (http://www.kymjs.com)
 */
public abstract class LiteActivity extends FragmentActivity
        implements View.OnClickListener, I_BroadcastReg, I_LiteActivity, I_SkipActivity {

    public static final int WHICH_MSG = 0X37210;

    public Activity aty;

    protected LiteFragment mCurrentLiteFragment;
    protected SupportFragment currentSupportFragment;
    private ThreadDataCallBack callback;
    private KJActivityHandle threadHandle = new KJActivityHandle(this);

    /**
     * Activity?
     */
    public int activityState = DESTROY;

    /**
     * ?????
     */
    private interface ThreadDataCallBack {
        void onSuccess();
    }

    private static class KJActivityHandle extends Handler {
        private final SoftReference<LiteActivity> mOuterInstance;

        KJActivityHandle(LiteActivity outer) {
            mOuterInstance = new SoftReference<>(outer);
        }

        // ?????
        @Override
        public void handleMessage(android.os.Message msg) {
            LiteActivity aty = mOuterInstance.get();
            if (msg.what == WHICH_MSG && aty != null) {
                aty.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;
        LiteActivityStack.create().addActivity(this);
        Loger.state(this.getClass().getName(), "---------onCreate ");

        setRootView(); // annotate?
        ButterKnife.bind(this);
        initializer();
        registerBroadcast();
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Loger.state(this.getClass().getName(), "---------onStart ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        activityState = RESUME;
        Loger.state(this.getClass().getName(), "---------onResume ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        activityState = PAUSE;
        Loger.state(this.getClass().getName(), "---------onPause ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        activityState = STOP;
        Loger.state(this.getClass().getName(), "---------onStop ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Loger.state(this.getClass().getName(), "---------onRestart ");
    }

    @Override
    protected void onDestroy() {
        unRegisterBroadcast();
        activityState = DESTROY;
        Loger.state(this.getClass().getName(), "---------onDestroy ");
        super.onDestroy();
        LiteActivityStack.create().finishActivity(this);
        mCurrentLiteFragment = null;
        currentSupportFragment = null;
        callback = null;
        threadHandle = 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, LiteFragment targetFragment) {
        if (targetFragment.equals(mCurrentLiteFragment)) {
            return;
        }
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            transaction.add(resView, targetFragment, targetFragment.getClass().getName());
        }
        if (targetFragment.isHidden()) {
            transaction.show(targetFragment);
            targetFragment.onChange();
        }
        if (mCurrentLiteFragment != null && mCurrentLiteFragment.isVisible()) {
            transaction.hide(mCurrentLiteFragment);
        }
        mCurrentLiteFragment = 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();
    }
}