Skip to content

Instantly share code, notes, and snippets.

@ajharry69
Created August 1, 2020 12:58
Show Gist options
  • Save ajharry69/a1d5af6fea3840c8d9b2d9665790acfb to your computer and use it in GitHub Desktop.
Save ajharry69/a1d5af6fea3840c8d9b2d9665790acfb to your computer and use it in GitHub Desktop.
Java overloaded implementation of launchFragmentInHiltContainer
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.testing.FragmentScenario.EmptyFragmentActivity;
import androidx.test.core.app.ActivityScenario;
import androidx.test.core.app.ApplicationProvider;
import path.to.your.R; // TODO: Replace with your own
import static java.util.Objects.requireNonNull;
public class HiltContainer {
public static <A extends FragmentActivity, F extends Fragment> void launchFragmentInHiltContainer(
@NonNull Class<A> activityClass, @NonNull Class<F> fragmentClass) {
launchFragmentInHiltContainer(activityClass, fragmentClass, null);
}
public static <A extends FragmentActivity, F extends Fragment> void launchFragmentInHiltContainer(
@NonNull Class<A> activityClass, @NonNull Class<F> fragmentClass, @Nullable Bundle fragmentArgs) {
launchFragmentInHiltContainer(activityClass, fragmentClass, fragmentArgs, R.style.AppTheme);
}
public static <A extends FragmentActivity, F extends Fragment> void launchFragmentInHiltContainer(
@NonNull Class<A> activityClass,
@NonNull Class<F> fragmentClass,
@Nullable Bundle fragmentArgs,
@StyleRes int themeResId) {
launchFragmentInHiltContainer(activityClass, fragmentClass, fragmentArgs, themeResId, null);
}
public static <A extends FragmentActivity, F extends Fragment> void launchFragmentInHiltContainer(
@NonNull Class<A> activityClass,
@NonNull Class<F> fragmentClass,
@Nullable Bundle fragmentArgs,
@StringRes int themeResId,
Function<F> action) {
ComponentName componentName = new ComponentName(ApplicationProvider.getApplicationContext(), activityClass);
Intent startActivityIntent = Intent.makeMainActivity(componentName)
.putExtra(EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY, themeResId);
ActivityScenario<A> activityScenario = ActivityScenario.launch(startActivityIntent);
activityScenario.onActivity(activity -> {
@SuppressWarnings("unchecked")
F fragment = (F) activity.getSupportFragmentManager().getFragmentFactory()
.instantiate(requireNonNull(fragmentClass.getClassLoader()), fragmentClass.getName());
fragment.setArguments(fragmentArgs);
activity.getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, fragment, "")
.commitNow();
action.apply(fragment);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment