Java Code Examples for android.support.v4.app.Fragment#getArguments()

The following examples show how to use android.support.v4.app.Fragment#getArguments() . 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: IntentUtils.java    From Shield with MIT License 6 votes vote down vote up
public static byte getByteParam(String name, byte defaultValue, Fragment fragment) {
    if (fragment.getArguments() != null && fragment.getArguments().containsKey(name)) {
        return fragment.getArguments().getByte(name);
    }

    Intent i = fragment.getActivity().getIntent();
    try {
        Uri uri = i.getData();
        if (uri != null) {
            String val = uri.getQueryParameter(name);
            if (!TextUtils.isEmpty(val))
                return Byte.parseByte(val);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return i.getByteExtra(name, defaultValue);
}
 
Example 2
Source File: FragmentUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
private static List<FragmentNode> getAllFragmentsInStack(@NonNull final FragmentManager fm,
                                                         final List<FragmentNode> result) {
    List<Fragment> fragments = getFragments(fm);
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null) {
            Bundle args = fragment.getArguments();
            if (args != null && args.getBoolean(ARGS_IS_ADD_STACK)) {
                result.add(new FragmentNode(fragment,
                        getAllFragmentsInStack(fragment.getChildFragmentManager(),
                                new ArrayList<FragmentNode>())));
            }
        }
    }
    return result;
}
 
Example 3
Source File: IntentUtils.java    From Shield with MIT License 6 votes vote down vote up
public static short getShortParam(String name, short defaultValue, Fragment fragment) {
    if (fragment.getArguments() != null && fragment.getArguments().containsKey(name)) {
        return fragment.getArguments().getShort(name);
    }
    Intent i = fragment.getActivity().getIntent();
    try {
        Uri uri = i.getData();
        if (uri != null) {
            String val = uri.getQueryParameter(name);
            if (!TextUtils.isEmpty(val))
                return Short.parseShort(val);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return i.getShortExtra(name, defaultValue);
}
 
Example 4
Source File: TransactionActivityTest.java    From budget-watch with GNU General Public License v3.0 6 votes vote down vote up
private void checkFragmentStatus(final Activity activity, final int adapterItemIndex)
{
    final ViewPager viewPager = (ViewPager) activity.findViewById(R.id.pager);
    assertNotNull(viewPager);
    final FragmentStatePagerAdapter adapter = (FragmentStatePagerAdapter)viewPager.getAdapter();
    assertNotNull(adapter);

    Fragment fragment = adapter.getItem(adapterItemIndex);
    assertNotNull(fragment);

    Bundle arguments = fragment.getArguments();
    assertNotNull(arguments);
    final int expectedTransactionType = (adapterItemIndex == 0) ?
            DBHelper.TransactionDbIds.EXPENSE : DBHelper.TransactionDbIds.REVENUE;
    assertEquals(expectedTransactionType, arguments.getInt("type"));
}
 
Example 5
Source File: AnnotationParser.java    From hoko-android with Apache License 2.0 6 votes vote down vote up
/**
 * Injects route parameters on to the fields of an fragment instance, from inbound arguments.
 * Uses a route to map parameters to keys and then to the correct fields on the fragment
 * object.
 *
 * @param fragment An annotated fragment.
 * @return true in case it injected values, false otherwise.
 */
public static boolean inject(Fragment fragment) {
    if (fragment.getArguments() == null)
        return false;

    String route = fragment.getArguments().getString(IntentRouteImpl.BUNDLE_KEY);
    if (route == null)
        return false;

    Bundle routeParametersBundle = fragment.getArguments()
            .getBundle(IntentRouteImpl.ROUTE_PARAMETERS_BUNDLE_KEY);
    Bundle queryParametersBundle = fragment.getArguments()
            .getBundle(IntentRouteImpl.QUERY_PARAMETERS_BUNDLE_KEY);
    String metadata = fragment.getArguments().getString(IntentRouteImpl.METADATA_KEY);

    return inject(fragment, route, routeParametersBundle, queryParametersBundle, metadata);
}
 
Example 6
Source File: FooSerializerClassTest.java    From pocketknife with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildFragmentAndBindArguments() {
    String bar = UUID.randomUUID().toString();
    int baz = random.nextInt();
    SerializerBuilder builder = new PocketKnifeSerializerBuilder(RuntimeEnvironment.application);
    Fragment fragment = builder.buildFragment(new Foo(bar, baz));
    Bundle bundle = fragment.getArguments();

    Foo foo = new Foo();
    FooSerializerClass fooSerializerClass = new FooSerializerClass();
    fooSerializerClass.argFoo = foo;
    PocketKnife.bindArguments(fooSerializerClass, bundle);

    assertTrue(foo == fooSerializerClass.argFoo); // Same memory address didn't get reassigned
    assertEquals(bar, foo.getBar());
    assertEquals(baz, foo.getBaz());
}
 
Example 7
Source File: IntentUtils.java    From Shield with MIT License 6 votes vote down vote up
/**
 * 先尝试获取getArguments()中的参数<br>
 * 如果getArguments()不包含指定参数,再从Activity的Uri中获取参数<br>
 * 如果Uri中不包含指定参数,则从Activity.getIntent()中获取参数<br>
 * 如果以上均获取不到参数,则返回defaultValue
 */

public static int getIntParam(String name, int defaultValue, Fragment fragment) {
    if (fragment.getArguments() != null && fragment.getArguments().containsKey(name)) {
        return fragment.getArguments().getInt(name);
    }
    Intent i = fragment.getActivity().getIntent();
    try {
        Uri uri = i.getData();
        if (uri != null) {
            String val = uri.getQueryParameter(name);
            if (!TextUtils.isEmpty(val)) {
                return Integer.parseInt(val);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return i.getIntExtra(name, defaultValue);
}
 
Example 8
Source File: IntentUtils.java    From Shield with MIT License 6 votes vote down vote up
public static boolean getBooleanParam(String name, boolean defaultValue, Fragment fragment) {
    if (fragment.getArguments() != null && fragment.getArguments().containsKey(name)) {
        return fragment.getArguments().getBoolean(name);
    }

    Intent i = fragment.getActivity().getIntent();
    try {
        Uri uri = i.getData();
        if (uri != null) {
            String val = uri.getQueryParameter(name);
            if (!TextUtils.isEmpty(val))
                return Boolean.parseBoolean(val);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return i.getBooleanExtra(name, defaultValue);
}
 
Example 9
Source File: IntentUtils.java    From Shield with MIT License 6 votes vote down vote up
public static String getStringParam(String name, Fragment fragment) {
    if (fragment.getArguments() != null && fragment.getArguments().containsKey(name)) {
        return fragment.getArguments().getString(name);
    }
    Intent i = fragment.getActivity().getIntent();
    try {
        Uri uri = i.getData();
        if (uri != null) {
            String val = uri.getQueryParameter(name);
            if (val != null)
                return val;
        }
    } catch (Exception e) {
    }

    return i.getStringExtra(name);
}
 
Example 10
Source File: FragmentUtils.java    From MVVMArms with Apache License 2.0 5 votes vote down vote up
/**
 * 替换fragment
 *
 * @param srcFragment   源fragment
 * @param destFragment  目标fragment
 * @param isAddStack    是否入回退栈
 * @param sharedElement 共享元素
 * @return 目标fragment
 */
public static Fragment replaceFragment(@NonNull Fragment srcFragment,
                                       @NonNull Fragment destFragment,
                                       boolean isAddStack,
                                       SharedElement... sharedElement) {
    if (srcFragment.getArguments() == null) {
        return null;
    }
    int containerId = srcFragment.getArguments().getInt(ARGS_ID);
    if (containerId == 0) {
        return null;
    }
    return replaceFragment(srcFragment.getFragmentManager(), destFragment, containerId, isAddStack, sharedElement);
}
 
Example 11
Source File: BackStackEntry.java    From android-multibackstack with Apache License 2.0 5 votes vote down vote up
@NonNull
public static BackStackEntry create(@NonNull FragmentManager fm, @NonNull Fragment f) {
  String fname = f.getClass().getName();
  SavedState state = fm.saveFragmentInstanceState(f);
  Bundle args = f.getArguments();
  return new BackStackEntry(fname, state, args);
}
 
Example 12
Source File: FragmentUtils.java    From TikTok with Apache License 2.0 5 votes vote down vote up
/**
 * 获取参数
 *
 * @param fragment fragment
 */
private static Args getArgs(@NonNull Fragment fragment) {
    Bundle bundle = fragment.getArguments();
    if (bundle == null || bundle.getInt(ARGS_ID) == 0)
        return null;
    return new Args(bundle.getInt(ARGS_ID), bundle.getBoolean(ARGS_IS_HIDE), bundle.getBoolean(ARGS_IS_ADD_STACK));
}
 
Example 13
Source File: FragmentUtils.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 替换fragment
 *
 * @param srcFragment  源fragment
 * @param destFragment 目标fragment
 * @param isAddStack   是否入回退栈
 * @return 目标fragment
 */
public static Fragment replaceFragment(@NonNull final Fragment srcFragment,
                                       @NonNull final Fragment destFragment,
                                       final boolean isAddStack) {
    if (srcFragment.getArguments() == null) return null;
    int containerId = srcFragment.getArguments().getInt(ARGS_ID);
    if (containerId == 0) return null;
    return replaceFragment(srcFragment.getFragmentManager(), destFragment, containerId, isAddStack);
}
 
Example 14
Source File: BuilderTest.java    From pocketknife with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFragment() throws Exception {
    int i = random.nextInt();
    Fragment fragment = builder.getFragment(i);
    assertNotNull("Fragment", fragment);
    Bundle args = fragment.getArguments();
    assertNotNull("Args", args);
    assertEquals(i, args.getInt(PocketKnifeBuilder.ARG_ARG, i << 1));

}
 
Example 15
Source File: FragmentLifecycle.java    From MVVMArms with Apache License 2.0 5 votes vote down vote up
@Override
public void onFragmentAttached(FragmentManager fm, Fragment f, Context context) {
    super.onFragmentAttached(fm, f, context);
    if (f instanceof IFragment && f.getArguments() != null) {
        Timber.i("%s ---> onFragmentAttached", f.toString());
        FragmentDelegate fragmentDelegate = fetchFragmentDelegate(f);
        if (fragmentDelegate == null || !fragmentDelegate.isAdded()) {
            fragmentDelegate = new FragmentDelegateImpl(fm, f);
            f.getArguments().putParcelable(FragmentDelegate.FRAGMENT_DELEGATE, fragmentDelegate);
        }
        fragmentDelegate.onAttach(context);
    }
}
 
Example 16
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 4 votes vote down vote up
/**
 * 获取参数
 *
 * @param fragment fragment
 */
private static Args getArgs(@NonNull Fragment fragment) {
    Bundle bundle = fragment.getArguments();
    if (bundle == null || bundle.getInt(ARGS_ID) == 0) return null;
    return new Args(bundle.getInt(ARGS_ID), bundle.getBoolean(ARGS_IS_HIDE), bundle.getBoolean(ARGS_IS_ADD_STACK));
}
 
Example 17
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 4 votes vote down vote up
/**
 * 操作fragment
 *
 * @param fragmentManager fragment管理器
 * @param srcFragment     源fragment
 * @param destFragment    目标fragment
 * @param type            操作类型
 * @param sharedElements  共享元素
 * @return destFragment
 */
private static Fragment operateFragment(@NonNull FragmentManager fragmentManager,
                                        Fragment srcFragment,
                                        @NonNull Fragment destFragment,
                                        int type,
                                        SharedElement... sharedElements) {
    if (srcFragment == destFragment) return null;
    if (srcFragment != null && srcFragment.isRemoving()) {
        LogUtils.e(srcFragment.getClass().getName() + " is isRemoving");
        return null;
    }
    String name = destFragment.getClass().getName();
    Bundle args = destFragment.getArguments();

    FragmentTransaction ft = fragmentManager.beginTransaction();
    if (sharedElements == null || sharedElements.length == 0) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {
        for (SharedElement element : sharedElements) {// 添加共享元素动画
            ft.addSharedElement(element.sharedElement, element.name);
        }
    }
    switch (type) {
        case TYPE_ADD_FRAGMENT:
            if (srcFragment != null) ft.hide(srcFragment);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_HIDE)) ft.hide(destFragment);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_REMOVE_FRAGMENT:
            ft.remove(destFragment);
            break;
        case TYPE_REMOVE_TO_FRAGMENT:
            List<Fragment> fragments = getFragments(fragmentManager);
            for (int i = fragments.size() - 1; i >= 0; --i) {
                Fragment fragment = fragments.get(i);
                if (fragment == destFragment) {
                    if (srcFragment != null) ft.remove(fragment);
                    break;
                }
                ft.remove(fragment);
            }
            break;
        case TYPE_REPLACE_FRAGMENT:
            ft.replace(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_POP_ADD_FRAGMENT:
            popFragment(fragmentManager);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_HIDE_FRAGMENT:
            ft.hide(destFragment);
            break;
        case TYPE_SHOW_FRAGMENT:
            ft.show(destFragment);
            break;
        case TYPE_HIDE_SHOW_FRAGMENT:
            ft.hide(srcFragment).show(destFragment);
            break;
    }
    ft.commitAllowingStateLoss();
    return destFragment;
}
 
Example 18
Source File: FragmentOpener.java    From pandroid with Apache License 2.0 4 votes vote down vote up
@Nullable
public static <T extends FragmentOpener> T getOpener(@NonNull Fragment fragment) {
    Bundle args = fragment.getArguments();
    return getOpener(args);
}
 
Example 19
Source File: FragmentUtils.java    From styT with Apache License 2.0 4 votes vote down vote up
/**
 * 操作fragment
 *
 * @param fragmentManager fragment管理器
 * @param srcFragment     源fragment
 * @param destFragment    目标fragment
 * @param type            操作类型
 * @param sharedElements  共享元素
 * @return destFragment
 */
private static Fragment operateFragment(@NonNull final FragmentManager fragmentManager,
                                        final Fragment srcFragment,
                                        @NonNull Fragment destFragment,
                                        final int type,
                                        final SharedElement... sharedElements) {
    if (srcFragment == destFragment) return null;
    if (srcFragment != null && srcFragment.isRemoving()) {
        //LogUtils.e(srcFragment.getClass().getName() + " is isRemoving");
        return null;
    }
    String name = destFragment.getClass().getName();
    Bundle args = destFragment.getArguments();

    FragmentTransaction ft = fragmentManager.beginTransaction();
    if (sharedElements == null || sharedElements.length == 0) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {
        for (SharedElement element : sharedElements) {// 添加共享元素动画
            ft.addSharedElement(element.sharedElement, element.name);
        }
    }
    switch (type) {
        case TYPE_HIDE_ADD_FRAGMENT:
            ft.hide(srcFragment);
        case TYPE_ADD_FRAGMENT:
            Fragment fragmentByTag = fragmentManager.findFragmentByTag(name);
            if (fragmentByTag != null) {
                destFragment = fragmentByTag;
            }
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_HIDE)) ft.hide(destFragment);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_REMOVE_FRAGMENT:
            ft.remove(destFragment);
            break;
        case TYPE_REMOVE_TO_FRAGMENT:
            List<Fragment> fragments = getFragments(fragmentManager);
            for (int i = fragments.size() - 1; i >= 0; --i) {
                Fragment fragment = fragments.get(i);
                if (fragment == destFragment) {
                    if (srcFragment != null) ft.remove(fragment);
                    break;
                }
                ft.remove(fragment);
            }
            break;
        case TYPE_REPLACE_FRAGMENT:
            ft.replace(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_POP_ADD_FRAGMENT:
            popFragment(fragmentManager);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_HIDE_FRAGMENT:
            ft.hide(destFragment);
            break;
        case TYPE_SHOW_FRAGMENT:
            ft.show(destFragment);
            break;
        case TYPE_HIDE_SHOW_FRAGMENT:
            ft.hide(srcFragment).show(destFragment);
            break;
    }
    ft.commitAllowingStateLoss();
    return destFragment;
}
 
Example 20
Source File: ChooseSyncDataPagerHelper.java    From PADListener with GNU General Public License v2.0 4 votes vote down vote up
private void addResultToFragmentArguments(Fragment fragment) {
	if (fragment.getArguments() == null) {
		fragment.setArguments(new Bundle());
	}
	fragment.getArguments().putSerializable(ARGUMENT_SYNC_RESULT_NAME, mResult);
}