am.util.mvp.AMFragment.java Source code

Java tutorial

Introduction

Here is the source code for am.util.mvp.AMFragment.java

Source

/*
 * Copyright (C) 2015 AlexMofer
 *
 * 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 am.util.mvp;

import android.app.Activity;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Fragment
 * Created by Alex on 2017/3/14.
 */
@SuppressWarnings("unused")
@RequiresApi(11)
public abstract class AMFragment extends Fragment {

    private LocalBroadcastManager mLocalBroadcastManager;// 
    /**
     * ?
     */
    private BroadcastReceiver mLocalBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            onReceiveLocalBroadcast(context, intent);
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final AMPresenter presenter = getPresenter();
        if (presenter != null) {
            presenter.onCreated(savedInstanceState);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(getContentViewLayout(inflater, container, savedInstanceState), container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initializeFragment(getActivity(), savedInstanceState);
        if (isLocalBroadcastEnable()) {
            mLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
            IntentFilter filter = new IntentFilter();
            onAddLocalAction(filter);
            mLocalBroadcastManager.registerReceiver(mLocalBroadcastReceiver, filter);
            onRegisteredLocalBroadcastReceiver();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        final AMPresenter presenter = getPresenter();
        if (presenter != null) {
            presenter.onStarted();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        final AMPresenter presenter = getPresenter();
        if (presenter != null) {
            presenter.onResumed();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        final AMPresenter presenter = getPresenter();
        if (presenter != null) {
            presenter.onPaused();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        final AMPresenter presenter = getPresenter();
        if (presenter != null) {
            presenter.onStopped();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        final AMPresenter presenter = getPresenter();
        if (presenter != null) {
            presenter.onSaveInstanceState(outState);
        }
    }

    @Override
    public void onDestroy() {
        if (!isLocalBroadcastUnregistered()) {
            mLocalBroadcastManager.unregisterReceiver(mLocalBroadcastReceiver);
            mLocalBroadcastManager = null;
            onUnregisteredLocalBroadcastReceiver();
        }
        super.onDestroy();
        final AMPresenter presenter = getPresenter();
        if (presenter != null) {
            presenter.onDestroyed();
            presenter.detach();
        }
    }

    /**
     * ?Layout
     *
     * @param inflater           
     * @param container          View
     * @param savedInstanceState ??
     * @return ?id
     */
    @LayoutRes
    protected abstract int getContentViewLayout(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState);

    /**
     * ?Fragment
     *
     * @param activity           Activity
     * @param savedInstanceState ??
     */
    protected abstract void initializeFragment(Activity activity, @Nullable Bundle savedInstanceState);

    /**
     * ?Presenter
     *
     * @return Presenter
     */
    protected AMPresenter getPresenter() {
        return null;
    }

    /**
     * ??
     * Fragment??
     *
     * @return ??
     */
    public boolean isLocalBroadcastEnable() {
        return false;
    }

    /**
     * ??
     *
     * @param filter ?
     */
    protected void onAddLocalAction(IntentFilter filter) {
    }

    /**
     * 
     *
     * @param context ?context
     * @param intent  ??
     */
    protected void onReceiveLocalBroadcast(Context context, Intent intent) {
    }

    /**
     * 
     */
    protected void onRegisteredLocalBroadcastReceiver() {
    }

    /**
     * ?
     */
    protected void onUnregisteredLocalBroadcastReceiver() {
    }

    /**
     * ??
     *
     * @return ??
     */
    public final boolean isLocalBroadcastUnregistered() {
        return mLocalBroadcastManager == null;
    }

    /**
     * IDView
     *
     * @param id  View ?ID
     * @param <V> View
     * @return ?IDView
     */
    public final <V extends View> V findViewById(int id) {
        final View view = getView();
        if (view == null) {
            // 
            throw new IllegalStateException("Fragment " + this + " has not created its view yet.");
        }
        return view.findViewById(id);
    }

    /**
     * ??
     *
     * @param action 
     */
    public final void sendLocalBroadcast(String action) {
        sendLocalBroadcast(new Intent(action));
    }

    /**
     * ??
     *
     * @param intent ?
     */
    public final void sendLocalBroadcast(Intent intent) {
        if (isLocalBroadcastUnregistered()) {
            return;
        }
        mLocalBroadcastManager.sendBroadcast(intent);
    }
}