public class

AppComponentFactory

extends android.app.AppComponentFactory

 java.lang.Object

↳android.app.AppComponentFactory

↳androidx.core.app.AppComponentFactory

Subclasses:

CoreComponentFactory

Gradle dependencies

compile group: 'androidx.core', name: 'core', version: '1.9.0-alpha04'

  • groupId: androidx.core
  • artifactId: core
  • version: 1.9.0-alpha04

Artifact androidx.core:core:1.9.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.core:core com.android.support:support-compat

Androidx class mapping:

androidx.core.app.AppComponentFactory android.support.v4.app.AppComponentFactory

Overview

Version of that works with androidx libraries. Note: This will only work on API 28+ and does not backport AppComponentFactory functionality.

Summary

Constructors
publicAppComponentFactory()

Methods
public final ActivityinstantiateActivity(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

public ActivityinstantiateActivityCompat(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

Allows application to override the creation of activities.

public final ApplicationinstantiateApplication(java.lang.ClassLoader cl, java.lang.String className)

public ApplicationinstantiateApplicationCompat(java.lang.ClassLoader cl, java.lang.String className)

Allows application to override the creation of the application object.

public final ContentProviderinstantiateProvider(java.lang.ClassLoader cl, java.lang.String className)

public ContentProviderinstantiateProviderCompat(java.lang.ClassLoader cl, java.lang.String className)

Allows application to override the creation of providers.

public final BroadcastReceiverinstantiateReceiver(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

public BroadcastReceiverinstantiateReceiverCompat(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

Allows application to override the creation of receivers.

public final ServiceinstantiateService(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

public ServiceinstantiateServiceCompat(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

Allows application to override the creation of services.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public AppComponentFactory()

Methods

public final Activity instantiateActivity(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

See also: AppComponentFactory.instantiateActivityCompat(ClassLoader, String, Intent)

public final Application instantiateApplication(java.lang.ClassLoader cl, java.lang.String className)

See also: AppComponentFactory.instantiateApplicationCompat(ClassLoader, String)

public final BroadcastReceiver instantiateReceiver(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

See also: AppComponentFactory.instantiateReceiverCompat(ClassLoader, String, Intent)

public final ContentProvider instantiateProvider(java.lang.ClassLoader cl, java.lang.String className)

See also: AppComponentFactory.instantiateProviderCompat(ClassLoader, String)

public final Service instantiateService(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

See also: AppComponentFactory.instantiateServiceCompat(ClassLoader, String, Intent)

public Application instantiateApplicationCompat(java.lang.ClassLoader cl, java.lang.String className)

Allows application to override the creation of the application object. This can be used to perform things such as dependency injection or class loader changes to these classes.

This method is only intended to provide a hook for instantiation. It does not provide earlier access to the Application object. The returned object will not be initialized as a Context yet and should not be used to interact with other android APIs.

Parameters:

cl: The default classloader to use for instantiation.
className: The class to be instantiated.

public Activity instantiateActivityCompat(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

Allows application to override the creation of activities. This can be used to perform things such as dependency injection or class loader changes to these classes.

This method is only intended to provide a hook for instantiation. It does not provide earlier access to the Activity object. The returned object will not be initialized as a Context yet and should not be used to interact with other android APIs.

Parameters:

cl: The default classloader to use for instantiation.
className: The class to be instantiated.
intent: Intent creating the class.

public BroadcastReceiver instantiateReceiverCompat(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

Allows application to override the creation of receivers. This can be used to perform things such as dependency injection or class loader changes to these classes.

Parameters:

cl: The default classloader to use for instantiation.
className: The class to be instantiated.
intent: Intent creating the class.

public Service instantiateServiceCompat(java.lang.ClassLoader cl, java.lang.String className, Intent intent)

Allows application to override the creation of services. This can be used to perform things such as dependency injection or class loader changes to these classes.

This method is only intended to provide a hook for instantiation. It does not provide earlier access to the Service object. The returned object will not be initialized as a Context yet and should not be used to interact with other android APIs.

Parameters:

cl: The default classloader to use for instantiation.
className: The class to be instantiated.
intent: Intent creating the class.

public ContentProvider instantiateProviderCompat(java.lang.ClassLoader cl, java.lang.String className)

Allows application to override the creation of providers. This can be used to perform things such as dependency injection or class loader changes to these classes.

This method is only intended to provide a hook for instantiation. It does not provide earlier access to the ContentProvider object. The returned object will not be initialized with a Context yet and should not be used to interact with other android APIs.

Parameters:

cl: The default classloader to use for instantiation.
className: The class to be instantiated.

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * 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 androidx.core.app;

import static androidx.core.app.CoreComponentFactory.checkCompatWrapper;

import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentProvider;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.lang.reflect.InvocationTargetException;

/**
 * Version of {@link android.app.AppComponentFactory} that works with androidx libraries.
 *
 * Note: This will only work on API 28+ and does not backport AppComponentFactory functionality.
 */
@RequiresApi(28)
public class AppComponentFactory extends android.app.AppComponentFactory {

    /**
     * @see #instantiateActivityCompat
     */
    @NonNull
    @Override
    public final Activity instantiateActivity(
            @NonNull ClassLoader cl, @NonNull String className, @Nullable Intent intent)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return checkCompatWrapper(instantiateActivityCompat(cl, className, intent));
    }

    /**
     * @see #instantiateApplicationCompat
     */
    @NonNull
    @Override
    public final Application instantiateApplication(
            @NonNull ClassLoader cl, @NonNull String className)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return checkCompatWrapper(instantiateApplicationCompat(cl, className));
    }

    /**
     * @see #instantiateReceiverCompat
     */
    @NonNull
    @Override
    public final BroadcastReceiver instantiateReceiver(
            @NonNull ClassLoader cl, @NonNull String className, @Nullable Intent intent)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return checkCompatWrapper(instantiateReceiverCompat(cl, className, intent));
    }

    /**
     * @see #instantiateProviderCompat
     */
    @NonNull
    @Override
    public final ContentProvider instantiateProvider(
            @NonNull ClassLoader cl, @NonNull String className)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return checkCompatWrapper(instantiateProviderCompat(cl, className));
    }

    /**
     * @see #instantiateServiceCompat
     */
    @NonNull
    @Override
    public final Service instantiateService(
            @NonNull ClassLoader cl, @NonNull String className, @Nullable Intent intent)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return checkCompatWrapper(instantiateServiceCompat(cl, className, intent));
    }

    /**
     * Allows application to override the creation of the application object. This can be used to
     * perform things such as dependency injection or class loader changes to these
     * classes.
     * <p>
     * This method is only intended to provide a hook for instantiation. It does not provide
     * earlier access to the Application object. The returned object will not be initialized
     * as a Context yet and should not be used to interact with other android APIs.
     *
     * @param cl        The default classloader to use for instantiation.
     * @param className The class to be instantiated.
     */
    public @NonNull Application instantiateApplicationCompat(@NonNull ClassLoader cl,
            @NonNull String className)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        try {
            return Class.forName(className, false, cl).asSubclass(Application.class)
                    .getDeclaredConstructor().newInstance();
        } catch (InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException("Couldn't call constructor", e);
        }
    }

    /**
     * Allows application to override the creation of activities. This can be used to
     * perform things such as dependency injection or class loader changes to these
     * classes.
     * <p>
     * This method is only intended to provide a hook for instantiation. It does not provide
     * earlier access to the Activity object. The returned object will not be initialized
     * as a Context yet and should not be used to interact with other android APIs.
     *
     * @param cl        The default classloader to use for instantiation.
     * @param className The class to be instantiated.
     * @param intent    Intent creating the class.
     */
    public @NonNull Activity instantiateActivityCompat(@NonNull ClassLoader cl,
            @NonNull String className, @Nullable Intent intent)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        try {
            return Class.forName(className, false, cl).asSubclass(Activity.class)
                    .getDeclaredConstructor().newInstance();
        } catch (InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException("Couldn't call constructor", e);
        }
    }

    /**
     * Allows application to override the creation of receivers. This can be used to
     * perform things such as dependency injection or class loader changes to these
     * classes.
     *
     * @param cl        The default classloader to use for instantiation.
     * @param className The class to be instantiated.
     * @param intent    Intent creating the class.
     */
    public @NonNull BroadcastReceiver instantiateReceiverCompat(@NonNull ClassLoader cl,
            @NonNull String className, @Nullable Intent intent)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        try {
            return Class.forName(className, false, cl).asSubclass(BroadcastReceiver.class)
                    .getDeclaredConstructor().newInstance();
        } catch (InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException("Couldn't call constructor", e);
        }
    }

    /**
     * Allows application to override the creation of services. This can be used to
     * perform things such as dependency injection or class loader changes to these
     * classes.
     * <p>
     * This method is only intended to provide a hook for instantiation. It does not provide
     * earlier access to the Service object. The returned object will not be initialized
     * as a Context yet and should not be used to interact with other android APIs.
     *
     * @param cl        The default classloader to use for instantiation.
     * @param className The class to be instantiated.
     * @param intent    Intent creating the class.
     */
    public @NonNull Service instantiateServiceCompat(@NonNull ClassLoader cl,
            @NonNull String className, @Nullable Intent intent)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        try {
            return Class.forName(className, false, cl).asSubclass(Service.class)
                    .getDeclaredConstructor().newInstance();
        } catch (InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException("Couldn't call constructor", e);
        }
    }

    /**
     * Allows application to override the creation of providers. This can be used to
     * perform things such as dependency injection or class loader changes to these
     * classes.
     * <p>
     * This method is only intended to provide a hook for instantiation. It does not provide
     * earlier access to the ContentProvider object. The returned object will not be initialized
     * with a Context yet and should not be used to interact with other android APIs.
     *
     * @param cl        The default classloader to use for instantiation.
     * @param className The class to be instantiated.
     */
    public @NonNull ContentProvider instantiateProviderCompat(@NonNull ClassLoader cl,
            @NonNull String className)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        try {
            return Class.forName(className, false, cl).asSubclass(ContentProvider.class)
                    .getDeclaredConstructor().newInstance();
        } catch (InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException("Couldn't call constructor", e);
        }
    }
}