com.umeng.comm.ui.activities.BaseFragmentActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.umeng.comm.ui.activities.BaseFragmentActivity.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Umeng, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.umeng.comm.ui.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

import com.umeng.comm.core.CommunitySDK;
import com.umeng.comm.core.beans.CommConfig;
import com.umeng.comm.core.constants.Constants;
import com.umeng.comm.core.imageloader.UMImageLoader;
import com.umeng.comm.core.impl.CommunityFactory;
import com.umeng.comm.core.nets.responses.AbsResponse;
import com.umeng.comm.core.nets.uitls.NetworkUtils;
import com.umeng.comm.core.sdkmanager.ImageLoaderManager;
import com.umeng.comm.core.utils.CommonUtils;
import com.umeng.comm.ui.fragments.CommunityMainFragment;
import com.umeng.comm.ui.utils.FontUtils;

/**
 * FragmentActivityActivity, ?FragmentManagertitle
 * 
 * @author mrsimple
 */
public class BaseFragmentActivity extends FragmentActivity {
    /**
     * Fragment?
     */
    protected FragmentManager mFragmentManager = null;
    /**
     * Fragmentparent view,?Fragment
     */
    protected int mFragmentContainer;
    /**
     * ?Fragment
     */
    public Fragment mCurrentFragment;

    protected UMImageLoader mImageLoader = ImageLoaderManager.getInstance().getCurrentSDK();
    protected CommunitySDK mSdkImpl;
    private int totalTime = 0;
    private boolean isFinish = false;
    private InputMethodManager mInputMan;
    protected CommunityMainFragment mFeedsFragment = new CommunityMainFragment();

    /**
     * Handler????
     */
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            View view = (View) msg.obj;
            // 
            if (msg.what == Constants.INPUT_METHOD_SHOW) {
                boolean result = mInputMan.showSoftInput(view, 0);
                if (!result && totalTime < Constants.LIMIT_TIME) {
                    totalTime += Constants.IDLE;
                    Message message = Message.obtain(msg);
                    mHandler.sendMessageDelayed(message, Constants.IDLE);
                } else if (!isFinish) {
                    totalTime = 0;
                    result = view.requestFocus();
                    isFinish = true;
                }
            } else if (msg.what == Constants.INPUT_METHOD_DISAPPEAR) {
                // ??
                mInputMan.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }

        }
    };

    public void showMainFeedFragment() {
        showFragment(mFeedsFragment);
    }

    public void initFragment(int container) {
        setFragmentContainerId(container);
        showFragment(mFeedsFragment);
    }

    // protected boolean consumeEventForFragment(int keyCode, KeyEvent event){
    // return mFeedsFragment.onKeyDown(keyCode, event);
    // }

    /**
     * </br>
     * 
     * @param view
     */
    public void showInputMethod(View view) {
        sendInputMethodMessage(Constants.INPUT_METHOD_SHOW, view);
    }

    /**
     * ??</br>
     * 
     * @param view
     */
    public void hideInputMethod(View view) {
        sendInputMethodMessage(Constants.INPUT_METHOD_DISAPPEAR, view);
    }

    /**
     * ??show or hide?</br>
     * 
     * @param type
     * @param view
     */
    private void sendInputMethodMessage(int type, View view) {
        Message message = mHandler.obtainMessage(type);
        message.obj = view;
        mHandler.sendMessage(message);
    }

    @Override
    protected void onCreate(Bundle arg0) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(arg0);
        mFragmentManager = getSupportFragmentManager();
        mInputMan = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mSdkImpl = CommunityFactory.getCommSDK(this);
    }

    /*
     * [ ?? ],?FragmentActivitybug
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {

    }

    @Override
    protected void onResume() {
        super.onResume();

        // SharedPreferences??
        CommConfig.getConfig().loginedUser = CommonUtils.getLoginUser(this);
        // 
        FontUtils.changeTypeface(getWindow().getDecorView());
    }

    /**
     * @param container fragmentid
     * @param fragment ?Fragment
     */
    public void addFragment(int container, Fragment fragment) {

        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        if (!isFragmentAdded(fragment)) {
            fragmentTransaction.add(container, fragment, fragment.getClass().getName()).commitAllowingStateLoss();
            mCurrentFragment = fragment;
        } else {
            fragmentTransaction.show(fragment).commitAllowingStateLoss();
        }

        mFragmentContainer = container;
    }

    /**
     * ??
     * 
     * @param container fragmentid
     */
    public void setFragmentContainerId(int container) {
        mFragmentContainer = container;
    }

    /**
     * Fragment??
     * 
     * @param fragment ???Fragment
     * @return
     */
    public boolean isFragmentAdded(Fragment fragment) {
        return fragment != null && mFragmentManager.findFragmentByTag(fragment.getClass().getName()) != null;
    }

    /**
     * fragmentid
     */
    private void checkContainer() {
        if (mFragmentContainer <= 0) {
            throw new RuntimeException(
                    "replaceFragment?setFragmentContainerId?fragment container id");
        }
    }

    /**
     * Fragment??
     * 
     * @param fragment
     */
    public void showFragment(Fragment fragmentShow) {
        showFragmentInContainer(mFragmentContainer, fragmentShow);
    }

    /**
     * fragmentShowcontainer,?mFragmentContainer
     * ?Fragment?Fragment
     * 
     * @param container
     * @param fragmentShow
     */
    public void showFragmentInContainer(int container, Fragment fragmentShow) {
        checkContainer();

        if (mCurrentFragment != fragmentShow) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            if (mCurrentFragment != null) {
                // ???Fragment
                transaction.hide(mCurrentFragment);
            }
            // ???Fragment
            if (mFragmentManager.findFragmentByTag(fragmentShow.getClass().getName()) == null) {
                transaction.add(container, fragmentShow, fragmentShow.getClass().getName());
            } else {
                transaction.show(fragmentShow);
            }
            transaction.commitAllowingStateLoss();
            mCurrentFragment = fragmentShow;
        }
    }

    /**
     * TODO  Fragment?Fragment
     * 
     * @param fragment
     */
    public void replaceFragment(Fragment fragment) {
        replaceFragment(fragment, false);
    }

    /**
     * @param fragment
     */
    public void replaceFragment(Fragment fragment, boolean isAddToBackStack) {
        checkContainer();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.replace(mFragmentContainer, fragment);
        if (isAddToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.commitAllowingStateLoss();
        mCurrentFragment = fragment;
    }

    /**
     * @param container
     * @param fragment
     */
    public void replaceFragment(int container, Fragment fragment) {
        checkContainer();
        if (mCurrentFragment != fragment) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            transaction.replace(container, fragment, fragment.getClass().getSimpleName());
            transaction.commitAllowingStateLoss();
            mCurrentFragment = fragment;
        }
    }

    /**
     * @param fragment
     */
    public void remove(Fragment fragment) {
        if (null != fragment) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            transaction.remove(fragment);
            transaction.commitAllowingStateLoss();
        }
    }

    /**
     * @param fragment
     */
    public void detach(Fragment fragment) {
        if (null != fragment) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            transaction.detach(fragment);
            transaction.commit();
        }
    }

    @Override
    protected void onDestroy() {
        // avoid memory leak
        mHandler.removeCallbacksAndMessages(null);
        if (mFeedsFragment != null) {
            mFeedsFragment.hideCommentLayoutAndInputMethod();
        }
        super.onDestroy();
    }

    public boolean handlerResponse(AbsResponse<?> response) {
        return NetworkUtils.handlerResponse(this.getApplicationContext(), response);
    }
}