Java Code Examples for androidx.fragment.app.fragment#instantiate()

The following examples show how to use androidx.fragment.app.fragment#instantiate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseDialogBuilder.java    From android-styled-dialogs with Apache License 2.0 6 votes vote down vote up
private BaseDialogFragment create() {
    final Bundle args = prepareArguments();

    final BaseDialogFragment fragment = (BaseDialogFragment) Fragment.instantiate(mContext, mClass.getName(), args);

    args.putBoolean(ARG_CANCELABLE_ON_TOUCH_OUTSIDE, mCancelableOnTouchOutside);

    args.putBoolean(ARG_USE_DARK_THEME, mUseDarkTheme);

    args.putBoolean(ARG_USE_LIGHT_THEME, mUseLightTheme);

    if (mTargetFragment != null) {
        fragment.setTargetFragment(mTargetFragment, mRequestCode);
    } else {
        args.putInt(ARG_REQUEST_CODE, mRequestCode);
    }
    fragment.setCancelable(mCancelable);
    return fragment;
}
 
Example 2
Source File: PreferenceActivity.java    From AndroidPreferenceActivity with Apache License 2.0 6 votes vote down vote up
/**
 * Shows the fragment, which is associated with a specific navigation preference.
 *
 * @param navigationPreference
 *         The navigation preference, whose fragment should be shown, as an instance of the
 *         class {@link NavigationPreference}. The navigation preference may not be null
 * @param arguments
 *         The arguments, which should be passed to the fragment, as an instance of the class
 *         {@link Bundle} or null, if the navigation preferences's extras should be used
 *         instead
 */
private void showPreferenceFragment(@NonNull final NavigationPreference navigationPreference,
                                    @Nullable final Bundle arguments) {
    if (arguments != null && navigationPreference.getExtras() != null) {
        arguments.putAll(navigationPreference.getExtras());
    }

    selectedPreferenceFragment = navigationPreference.getFragment();
    selectedPreferenceFragmentArguments =
            arguments != null ? arguments : navigationPreference.getExtras();

    if (!TextUtils.isEmpty(selectedPreferenceFragment)) {
        Fragment fragment = Fragment.instantiate(this, navigationPreference.getFragment(),
                selectedPreferenceFragmentArguments);
        showPreferenceFragment(navigationPreference, fragment);
        showBreadCrumb(navigationPreference, selectedPreferenceFragmentArguments);
    } else {
        removePreferenceFragmentUnconditionally();

        if (isSplitScreen()) {
            showBreadCrumb(navigationPreference, selectedPreferenceFragmentArguments);
        }
    }

    adaptWizardButtonVisibilities();
}
 
Example 3
Source File: PreferenceActivity.java    From AndroidPreferenceActivity with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes the activity's fragments.
 */
private void initializeFragments() {
    navigationFragment = (NavigationFragment) getSupportFragmentManager()
            .findFragmentByTag(NAVIGATION_FRAGMENT_TAG);

    if (navigationFragment == null) {
        navigationFragment = (NavigationFragment) Fragment
                .instantiate(this, NavigationFragment.class.getName());
        navigationFragment.setRetainInstance(true);
        navigationFragment.setCallback(this);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.navigation_fragment_container, navigationFragment,
                NAVIGATION_FRAGMENT_TAG);
        transaction.commit();
    } else if (!navigationFragment.isAdapterCreated()) {
        navigationFragment.setCallback(this);
    }

    navigationFragment.setAdapterCallback(this);
    preferenceFragment = getSupportFragmentManager().findFragmentByTag(PREFERENCE_FRAGMENT_TAG);
    adaptNavigationSelectionColor();
    adaptNavigationDividerColor();
    adaptNavigationEnabledState();
}
 
Example 4
Source File: ReadabilityFragmentLazyLoadTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    TestApplication.applicationGraph.inject(this);
    reset(readabilityClient);
    controller = Robolectric.buildActivity(TestReadabilityActivity.class);
    activity = controller.create().start().resume().visible().get();
    PreferenceManager.getDefaultSharedPreferences(activity)
            .edit()
            .putString(activity.getString(R.string.pref_story_display),
                    activity.getString(R.string.pref_story_display_value_readability))
            .apply();
    Bundle args = new Bundle();
    WebItem item = new TestWebItem() {
        @Override
        public String getId() {
            return "1";
        }

        @Override
        public String getUrl() {
            return "http://example.com/article.html";
        }
    };
    args.putParcelable(WebFragment.EXTRA_ITEM, item);
    fragment = (WebFragment) Fragment.instantiate(activity, WebFragment.class.getName(), args);
}
 
Example 5
Source File: WebActivity.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        WebItem item = getIntent().getParcelableExtra(EXTRA_ITEM);
        Bundle args = new Bundle();
        args.putParcelable(WebFragment.EXTRA_ITEM, item);
        fragment = (WebFragment) Fragment.instantiate(this, WebFragment.class.getName(), args);
        getSupportFragmentManager()
                .beginTransaction()
                .add(android.R.id.content,
                        fragment,
                        WebFragment.class.getName())
                .commit();
    } else {
        fragment = (WebFragment) getSupportFragmentManager().findFragmentByTag(WebFragment.class.getName());
    }
}
 
Example 6
Source File: ItemPagerAdapter.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Override
public Fragment getItem(int position) {
    if (mFragments[position] != null) {
        return mFragments[position];
    }
    String fragmentName;
    Bundle args = new Bundle();
    args.putBoolean(LazyLoadFragment.EXTRA_EAGER_LOAD, mDefaultItem == position);
    if (position == 0) {
        args.putParcelable(ItemFragment.EXTRA_ITEM, mItem);
        args.putInt(ItemFragment.EXTRA_CACHE_MODE, mCacheMode);
        args.putBoolean(ItemFragment.EXTRA_RETAIN_INSTANCE, mRetainInstance);
        fragmentName = ItemFragment.class.getName();
    } else {
        args.putParcelable(WebFragment.EXTRA_ITEM, mItem);
        args.putBoolean(WebFragment.EXTRA_RETAIN_INSTANCE, mRetainInstance);
        fragmentName = WebFragment.class.getName();
    }
    return Fragment.instantiate(mContext, fragmentName, args);
}
 
Example 7
Source File: FragmentSwitcher.java    From zone-sdk with MIT License 6 votes vote down vote up
public void initFragment(Class... fragments){
    for (int i = 0; i < fragments.length; i++) {
        Class fragment = fragments[i];
        if(!Fragment.class.isAssignableFrom(fragment))
            throw new IllegalArgumentException("类型不是frament 不能展示");
        else{
            FragmentEntity entity = new FragmentEntity();
            entity.fragment=Fragment.instantiate(frameActivity,fragment.getName());
            if((ani_in_pri!=-1||ani_out_pri!=-1)){
                entity.ani_in=ani_in_pri;
                entity.ani_out=ani_out_pri;
            }
            entity.tag=TAG+i;
            fragmentEntityList.add(entity);
        }
    }
    init=true;
}
 
Example 8
Source File: NavigationActivity.java    From SmartPack-Kernel-Manager with GNU General Public License v3.0 5 votes vote down vote up
private Fragment getFragment(int res) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(res + "_key");
    if (fragment == null && mActualFragments.containsKey(res)) {
        fragment = Fragment.instantiate(this, Objects.requireNonNull(Objects.requireNonNull(mActualFragments.get(res)).getCanonicalName()));
    }
    return fragment;
}
 
Example 9
Source File: MainActivity.java    From AndroidMaterialPreferences with Apache License 2.0 5 votes vote down vote up
@Override
protected final void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);

    if (fragment == null) {
        fragment = Fragment.instantiate(this, PreferenceFragment.class.getName());
    }

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, fragment);
    transaction.commit();
}
 
Example 10
Source File: PopularActivity.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Override
protected Fragment instantiateListFragment() {
    Bundle args = new Bundle();
    setRange(Preferences.getPopularRange(this));
    args.putString(ListFragment.EXTRA_FILTER, mRange);
    args.putString(ListFragment.EXTRA_ITEM_MANAGER, AlgoliaPopularClient.class.getName());
    return Fragment.instantiate(this, ListFragment.class.getName(), args);
}
 
Example 11
Source File: PopupSettingsFragment.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState == null) {
        Fragment fragment = Fragment.instantiate(getActivity(),
                PreferenceFragment.class.getName(), getArguments());
        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.content, fragment)
                .commit();
    }
}
 
Example 12
Source File: ViewPagerAdapter.java    From AndroidMaterialDialog with Apache License 2.0 5 votes vote down vote up
@Override
public final Fragment getItem(final int index) {
    ViewPagerItem item = items.get(index);
    Class<? extends Fragment> fragmentClass = item.getFragmentClass();
    Bundle arguments = item.getArguments();
    return Fragment.instantiate(context, fragmentClass.getName(), arguments);
}
 
Example 13
Source File: NavigationActivity.java    From MTweaks-KernelAdiutorMOD with GNU General Public License v3.0 5 votes vote down vote up
private Fragment getFragment(int res) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(res + "_key");
    if (fragment == null && mActualFragments.containsKey(res)) {
        fragment = Fragment.instantiate(this,
                mActualFragments.get(res).getCanonicalName());
    }
    return fragment;
}
 
Example 14
Source File: MainActivity.java    From AndroidMaterialDialog with Apache License 2.0 5 votes vote down vote up
@Override
protected final void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG) == null) {
        fragment = (PreferenceFragment) Fragment
                .instantiate(this, PreferenceFragment.class.getName());
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment, fragment, FRAGMENT_TAG);
        transaction.commit();
    }

    initializeFloatingActionButton();
}
 
Example 15
Source File: FragmentPagerItem.java    From SmartTabLayout with Apache License 2.0 4 votes vote down vote up
public Fragment instantiate(Context context, int position) {
  setPosition(args, position);
  return Fragment.instantiate(context, className, args);
}
 
Example 16
Source File: TabPageAdapter.java    From AndroidAPS with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
@Nullable
public Fragment getItem(int position) {
    //Fragment fragment = (Fragment) visibleFragmentList.get(position);
    return Fragment.instantiate(context, visibleFragmentList.get(position).pluginDescription.getFragmentClass());
}
 
Example 17
Source File: FavoriteActivity.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Override
protected Fragment instantiateListFragment() {
    Bundle args = new Bundle();
    args.putString(FavoriteFragment.EXTRA_FILTER, mFilter);
    return Fragment.instantiate(this, FavoriteFragment.class.getName(), args);
}
 
Example 18
Source File: ViewPagerFragmentAdapter.java    From BaseProject with Apache License 2.0 4 votes vote down vote up
@Override
public Fragment getItem(int position) {
    ViewPagerFragmentInfo curFragmentInfo = fragmentPagers.get(position);
    return Fragment.instantiate(mContext, curFragmentInfo.fragmentClass.getName(), curFragmentInfo.extraBundleData);
}
 
Example 19
Source File: MusicLibraryPagerAdapter.java    From Phonograph with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Fragment getItem(final int position) {
    final Holder mCurrentHolder = mHolderList.get(position);
    return Fragment.instantiate(mContext,
            mCurrentHolder.mClassName, mCurrentHolder.mParams);
}
 
Example 20
Source File: TabsAdapter.java    From Kore with Apache License 2.0 4 votes vote down vote up
@Override
public Fragment getItem(int position) {
    TabInfo info = tabInfos.get(position);
    return Fragment.instantiate(context, info.fragmentClass.getName(), info.args);
}