Android Open Source - khandroid Host Fragment






From Project

Back to project page khandroid.

License

The source code is released under:

Apache License

If you think the Android project khandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2012-2014 Ognyan Bankov
 *//  w ww .j av a 2  s  .co  m
 * 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.github.khandroid.ui_functionality;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;


public class HostFragment extends Fragment implements FragmentAttachable.HostingAble {
    public static final int EVENT_ON_ATTACH = 0;
    public static final int EVENT_ON_CREATE = 1;
    public static final int EVENT_ON_ACTIVITY_CREATED = 2;
    public static final int EVENT_ON_START = 3;
    public static final int EVENT_ON_RESUME = 4;
    public static final int EVENT_ON_PAUSE = 5;
    public static final int EVENT_ON_STOP = 6;
    public static final int EVENT_ON_DESTROY = 7;
    public static final int EVENT_ON_DETACH = 8;
    public static final int EVENT_ON_SAVE_INSTANCE_STATE = 9;

    private ArrayList<FragmentAttachable> mAttachments = new ArrayList<FragmentAttachable>();


    /**
     * Override this if you will have only one attachable functionality. Returned attachable will be automatically
     * attached to this fragment and fragment this fragment will be attached to the attachable. {@see
     * #createAttachables()}
     * 
     * @return
     */

    public FragmentAttachable createTheOnlyAttachable() {
        return null;
    }


    /**
     * Override this if you need more than one attachable. Returned attachables will be automatically
     * attached to this fragment and fragment this fragment will be attached to the attachables. {@see
     * #createTheOnlyAttachable()}
     * 
     * @return
     */
    public ArrayList<FragmentAttachable> createAttachables() {
        return null;
    }


    /**
     * @deprecated Please use {@link #createAttachables()} or {@link #createTheOnlyAttachable()}
     * @param attachable
     */
    @Deprecated
    protected void attach(FragmentAttachable attachable) {
        attachInternal(attachable);
    }


    private void attachInternal(FragmentAttachable attachable) {
        if (attachable instanceof FragmentUniqueAttachedFunctionality) {
            if (isUniqueForMe((FragmentUniqueAttachedFunctionality) attachable)) {
                mAttachments.add(attachable);
                attachable.attachTo(this);
            } else {
                throw new IllegalArgumentException("Attempt to attach second FragmentAttachable of type "
                        + attachable.getClass());
            }
        } else {
            mAttachments.add(attachable);
            attachable.attachTo(this);
        }
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_ATTACH, null);
        }
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentAttachable only = createTheOnlyAttachable();
        if (only != null) {
            attachInternal(only);
        }

        ArrayList<FragmentAttachable> attachables = createAttachables();
        if (attachables != null) {
            for (FragmentAttachable fa : attachables) {
                attachInternal(fa);
            }
        }

        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_CREATE, savedInstanceState);
        }
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_ACTIVITY_CREATED, savedInstanceState);
        }
    }


    @Override
    public void onStart() {
        super.onStart();

        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_START, null);
        }
    }


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

        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_RESUME, null);
        }
    }


    @Override
    public void onPause() {
        super.onPause();

        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_PAUSE, null);
        }
    }


    @Override
    public void onStop() {
        super.onStop();

        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_STOP, null);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_DESTROY, null);
        }
    }


    @Override
    public void onDetach() {
        super.onDetach();

        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_DETACH, null);
        }
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        for (FragmentAttachable a : mAttachments) {
            a.fragmentLifeCycleEvent(EVENT_ON_SAVE_INSTANCE_STATE, outState);
        }
    }


    private boolean isUniqueForMe(FragmentUniqueAttachedFunctionality a) {
        boolean ret = true;

        for (FragmentAttachable a2 : mAttachments) {
            Class<? extends FragmentUniqueAttachedFunctionality> aClass = a.getClass();
            Class<? extends FragmentAttachable> a2Class = a2.getClass();

            if (aClass.equals(a2Class)) {
                ret = false;
            } else if (aClass.isAssignableFrom(a2Class) || a2Class.isAssignableFrom(aClass)) {
                ret = false;
            }
        }

        return ret;
    }
}




Java Source Code List

com.github.khandroid.JsonFunctionality.java
com.github.khandroid.http.functionality.HttpFunctionalityImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookiesImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookies.java
com.github.khandroid.http.functionality.HttpFunctionality.java
com.github.khandroid.http.misc.FileDownloader.java
com.github.khandroid.http.misc.KhandroidBasicResponseHandler.java
com.github.khandroid.http.misc.SynchronizedCookieStore.java
com.github.khandroid.http.request.GetRequestBuilder.java
com.github.khandroid.http.request.PostRequestBuilder.java
com.github.khandroid.http.request.RequestBuilder.java
com.github.khandroid.http.ssl.DefaultSslHttpClient.java
com.github.khandroid.http.ssl.KhandroidX509TrustManager.java
com.github.khandroid.http.ssl.SslSocketFactory.java
com.github.khandroid.kat.ActivityKatExecutorFunctionality.java
com.github.khandroid.kat.FragmentKatExecutorFunctionality.java
com.github.khandroid.kat.KatExecutor.java
com.github.khandroid.kat.KhandroidAsyncTask.java
com.github.khandroid.misc.ActivityUtils.java
com.github.khandroid.misc.DateTimeHelper.java
com.github.khandroid.misc.ImageUtils.java
com.github.khandroid.misc.LocationUtils.java
com.github.khandroid.misc.NetUtils.java
com.github.khandroid.misc.StringUtils.java
com.github.khandroid.misc.ViewUtils.java
com.github.khandroid.rest.InvalidJsonString.java
com.github.khandroid.rest.InvalidResponseException.java
com.github.khandroid.rest.KhRestExchangeBuilder.java
com.github.khandroid.rest.KhRestFunctionalityImpl.java
com.github.khandroid.rest.KhRestFunctionality.java
com.github.khandroid.rest.KhRestResult.java
com.github.khandroid.rest.MalformedResponseException.java
com.github.khandroid.rest.RawRestResult.java
com.github.khandroid.rest.RestAsyncTask.java
com.github.khandroid.rest.RestExchangeBuilder.java
com.github.khandroid.rest.RestExchangeFailedException.java
com.github.khandroid.rest.RestExchangeOutcome.java
com.github.khandroid.rest.RestExchange.java
com.github.khandroid.rest.RestFragmentFunctionality.java
com.github.khandroid.rest.RestFunctionalityImpl.java
com.github.khandroid.rest.RestFunctionality.java
com.github.khandroid.rest.RestPersistFragment.java
com.github.khandroid.rest.RestResponse.java
com.github.khandroid.rest.RestResult.java
com.github.khandroid.rest.UnexpectedResponseException.java
com.github.khandroid.state.StateEvent.java
com.github.khandroid.state.StateMachineImpl.java
com.github.khandroid.state.StateMachine.java
com.github.khandroid.state.StateSkeleton.java
com.github.khandroid.state.State.java
com.github.khandroid.state_app.AppStateContextImpl.java
com.github.khandroid.state_app.AppStateContext.java
com.github.khandroid.state_app.AppState.java
com.github.khandroid.state_app.InvalidAppStateException.java
com.github.khandroid.state_app.SkeletonAppState.java
com.github.khandroid.state_app.StateEvent.java
com.github.khandroid.ui_functionality.ActivityAttachable.java
com.github.khandroid.ui_functionality.ActivityAttachedFunctionality.java
com.github.khandroid.ui_functionality.ActivityUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentAttachable.java
com.github.khandroid.ui_functionality.FragmentAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.HostActivity.java
com.github.khandroid.ui_functionality.HostFragment.java
com.github.khandroid.ui_functionality.SuperNotCalledException.java
com.github.khandroid.views.ImageViewWithContextMenuInfo.java
com.github.khandroid.volley.GetJsonObjectRequestBuilder.java
com.github.khandroid.volley.GetStringRequestBuilder.java
com.github.khandroid.volley.GetVRestRequestBuilder.java
com.github.khandroid.volley.PostJsonObjectRequestBuilder.java
com.github.khandroid.volley.PostRequestBuilderImpl.java
com.github.khandroid.volley.PostRequestBuilder.java
com.github.khandroid.volley.PostStringRequestBuilder.java
com.github.khandroid.volley.PostVRestRequestBuilder.java
com.github.khandroid.volley.RequestBuilder.java
com.github.khandroid.volley.VRestRequestBuilder.java
com.github.khandroid.volley.VRestRequest.java