android.app.Fragment Java Examples

The following examples show how to use android.app.Fragment. 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: CustomHeadersFragment.java    From BuildingForAndroidTV with MIT License 6 votes vote down vote up
@Override
public void onActivityCreated(Bundle savedInstanceState) {
	super.onActivityCreated(savedInstanceState);

	customSetBackground(R.color.fastlane_background);
	setHeaderAdapter();

	setCustomPadding();

	/**
	 * The setOnItemSelectedListener has been not only deprecated, but brutally removed by
	 * Google. To get around this limitation, I went to see how BaseRowFragment handled it.
	 * Turns out it sets a listener to the GridView (which is a RecyclerView): there you go.
	 */
	VerticalGridView gridView = ((MainActivity) getActivity()).getVerticalGridView(this);
	gridView.setOnChildSelectedListener(new OnChildSelectedListener() {
		@Override
		public void onChildSelected(ViewGroup viewGroup, View view, int i, long l) {
			Object obj = ((ListRow) getAdapter().get(i)).getAdapter().get(0);
			getFragmentManager().beginTransaction().replace(R.id.rows_container, (Fragment) obj).commit();
			((MainActivity) getActivity()).updateCurrentFragment((Fragment) obj);
		}
	});

}
 
Example #2
Source File: EncryptionParamsActivity.java    From oversec with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        finish();
        setupExitTransition();
        restoreImeStateOnBackPressed();
        return true;
    } else if (id == R.id.action_share_app) {
        Share.share(this);
        return true;
    } else if (id == R.id.action_pgp_choose_own_key) {
        mGpgEncryptionParamsFragment.triggerSigningKeySelection(null);
        return true;
    } else if (id == R.id.help) {
        Fragment f = mTabAdapter.getItem(mTitleIndicator.getSelectedTabPosition());
        Help.ANCHOR a = null;
        if (f instanceof WithHelp) {
            a = ((WithHelp) f).getHelpAnchor();
        }
        Help.INSTANCE.open(this, a);

        return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example #3
Source File: RxFragmentLifecycleTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testBindUntilEvent(LifecycleProvider<FragmentEvent> provider) {
    Fragment fragment = (Fragment) provider;
    startFragment(fragment);

    TestObserver<Object> testObserver = observable.compose(provider.bindUntilEvent(STOP)).test();

    fragment.onAttach(null);
    testObserver.assertNotComplete();
    fragment.onCreate(null);
    testObserver.assertNotComplete();
    fragment.onViewCreated(null, null);
    testObserver.assertNotComplete();
    fragment.onStart();
    testObserver.assertNotComplete();
    fragment.onResume();
    testObserver.assertNotComplete();
    fragment.onPause();
    testObserver.assertNotComplete();
    fragment.onStop();
    testObserver.assertComplete();
}
 
Example #4
Source File: SearchActivity.java    From Paperwork-Android with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(toolbar);

    if (getSupportActionBar() != null)
    {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    mNotesFragment = (NotesFragment) Fragment.instantiate(SearchActivity.this, NotesFragment.class.getName());
    Bundle bundle = new Bundle();
    bundle.putBoolean(NotesFragment.KEY_SEARCH_MODE, true);
    mNotesFragment.setArguments(bundle);
    (getFragmentManager().beginTransaction().replace(R.id.main_container, mNotesFragment)).commit();

    SearchView searchView = (SearchView) findViewById(R.id.search);
    searchView.onActionViewExpanded();
    searchView.setOnQueryTextListener(this);
}
 
Example #5
Source File: MainActivity.java    From catnut with MIT License 6 votes vote down vote up
/**
 * 切换fragment,附带一个动画效果
 *
 * @param fragment
 * @param tag      没有赋null即可
 */
private void pendingFragment(Fragment fragment, String tag) {
	FragmentManager fragmentManager = getFragmentManager();
	Fragment tmp = fragmentManager.findFragmentByTag(tag);
	if (tmp == null || !tmp.isVisible()) {
		fragmentManager
				.beginTransaction()
				.setCustomAnimations(
						R.animator.fragment_slide_left_enter,
						R.animator.fragment_slide_left_exit,
						R.animator.fragment_slide_right_enter,
						R.animator.fragment_slide_right_exit
				)
				.replace(R.id.fragment_container, fragment, tag)
				.addToBackStack(null)
				.commit();
		mScrollSettleHandler.post(new Runnable() {
			@Override
			public void run() {
				invalidateOptionsMenu();
			}
		});
	}
}
 
Example #6
Source File: NavigationDrawerActivity.java    From ALLGO with Apache License 2.0 6 votes vote down vote up
private void selectItem(int position) {
    // update the main content by replacing fragments
    Fragment fragment = new SampleFragment();
    Bundle args = new Bundle();
    args.putInt(SampleFragment.ARG_IMAGE_RES, mCityImages[position]);
    args.putInt(SampleFragment.ARG_ACTION_BG_RES, R.drawable.ab_background);
    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mCityNames[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}
 
Example #7
Source File: FirstRunPagerAdapter.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
@Override
public Fragment getItem(int position) {
    assert position >= 0 && position < mPages.size();
    FirstRunPage result = null;
    try {
        result = mPages.get(position).call();
    } catch (Exception e) {
        // We can always return null and it will be properly handled at the caller level.
    }
    if (result == null) return null;

    Bundle props = new Bundle();
    props.putAll(mFreProperties);
    result.setArguments(props);

    return result;
}
 
Example #8
Source File: ScreenSlidePagerAdapter.java    From prevent with Do What The F*ck You Want To Public License 5 votes vote down vote up
@Override
public Fragment getItem(int position) {
    if (position == 0) {
        return new Applications();
    } else if (position == 1) {
        return new PreventList();
    } else {
        return null;
    }
}
 
Example #9
Source File: HelpUtils.java    From BetterWeather with Apache License 2.0 5 votes vote down vote up
public static void showAboutDialog(Activity activity) {
    FragmentManager fm = activity.getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("dialog_about");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    new AboutDialog().show(ft, "dialog_about");
}
 
Example #10
Source File: AlarmArrayAdapter.java    From openvidonn with GNU General Public License v3.0 5 votes vote down vote up
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
    alarm.setTime(hourOfDay, minute);
    Fragment targetFragment = getTargetFragment();
    if (targetFragment != null) {
        targetFragment.onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
    }
}
 
Example #11
Source File: MainActivity.java    From xposed-gps with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	settings = new Settings(getApplicationContext());
	FragmentManager fm = getFragmentManager();
	Fragment frag = fm.findFragmentById(R.id.map);
	if (frag instanceof MapFragment) {
		MapFragment mapf = (MapFragment) frag;
		mMap = (GoogleMap) mapf.getMap();
		mMap.setOnCameraChangeListener(this);
		mMarker = new MarkerOptions();
		mInit = new LatLng(settings.getLat(), settings.getLng());
		mMarker.position(mInit);
		mMarker.draggable(true);

		CameraUpdate cam = CameraUpdateFactory.newLatLng(mInit);
		mMap.moveCamera(cam);
		mMap.addMarker(mMarker);
	}

	Button set   = (Button) findViewById(R.id.set_location);
	set.setOnClickListener(this);
	Button start = (Button) findViewById(R.id.start);
	start.setOnClickListener(this);
	Button sel   = (Button) findViewById(R.id.select_apps);
	sel.setOnClickListener(this);

	start.setText(settings.isStarted() ? getString(R.string.stop) : getString(R.string.start));
}
 
Example #12
Source File: DialtactsActivity.java    From coursera-android with MIT License 5 votes vote down vote up
private Fragment getFragmentAt(int position) {
    switch (position) {
        case TAB_INDEX_DIALER:
            return mDialpadFragment;
        case TAB_INDEX_CALL_LOG:
            return mCallLogFragment;
        case TAB_INDEX_FAVORITES:
            return mPhoneFavoriteFragment;
        default:
            throw new IllegalStateException("Unknown fragment index: " + position);
    }
}
 
Example #13
Source File: DemoActivity.java    From ngAndroid with Apache License 2.0 5 votes vote down vote up
private Fragment getFragment(Class<? extends Fragment> fragClass){
    try {
        return fragClass.newInstance();
    } catch (Exception e) {
        Toast.makeText(this, "Error showing " + fragClass.getSimpleName().replace("Fragment", ""), Toast.LENGTH_SHORT).show();
        Log.e("DemoScope", "Unable to create Fragment", e);
        return null;
    }
}
 
Example #14
Source File: FragmentStatePagerAdapter.java    From BlackLight with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
    Fragment fragment = (Fragment)object;
    if (fragment != mCurrentPrimaryItem) {
        if (mCurrentPrimaryItem != null) {
            mCurrentPrimaryItem.setMenuVisibility(false);
            mCurrentPrimaryItem.setUserVisibleHint(false);
        }
        if (fragment != null) {
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
        }
        mCurrentPrimaryItem = fragment;
    }
}
 
Example #15
Source File: InstrumentationHook.java    From ACDD with MIT License 5 votes vote down vote up
ExecStartFrgmentImpl_JELLY_BEAN(Context context, IBinder contextThread, IBinder token, Fragment fragment, Intent intent,
                                int requestCode, Bundle options) {
    this.who = context;
    this.contextThread = contextThread;
    this.token = token;
    this.target = fragment;
    this.intent = intent;
    this.requestCode = requestCode;
    this.options = options;
}
 
Example #16
Source File: RootActivity.java    From Depth with MIT License 5 votes vote down vote up
public void goToFragment(final Fragment newFragment) {
    getFragmentManager().beginTransaction().add(R.id.fragment_container, newFragment).commit();
    final Fragment removeFragment = currentFragment;
    currentFragment = newFragment;
    getWindow().getDecorView().postDelayed(new Runnable() {
        @Override
        public void run() {
            getFragmentManager().beginTransaction().remove(removeFragment).commit();
        }
    }, 2000);
}
 
Example #17
Source File: OAuthActivity.java    From box-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void onDifferentAuthenticationChosen() {
    Fragment fragment = getFragmentManager().findFragmentByTag(CHOOSE_AUTH_TAG);
    if (fragment != null){
        getFragmentManager().popBackStack();
    }
}
 
Example #18
Source File: FaBoArduinoActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * サービスとバインドされたことをを通知します.
 */
private void notifyBindService() {
    FragmentManager manager = getFragmentManager();
    Fragment f = manager.findFragmentById(R.id.fragment_container);
    if (f != null && f instanceof FaBoArduinoFragment) {
        ((FaBoArduinoFragment) f).onBindService();
    }

}
 
Example #19
Source File: QuickReturnTwitterActivity.java    From QuickReturn with Apache License 2.0 5 votes vote down vote up
@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return QuickReturnTwitterFragment.newInstance();
        case 1:
            return QuickReturnTwitterFragment.newInstance();
        case 2:
            return QuickReturnTwitterFragment.newInstance();
        default:
            return QuickReturnTwitterFragment.newInstance();
    }
}
 
Example #20
Source File: HostActivityLauncher.java    From Router-RePlugin with Apache License 2.0 5 votes vote down vote up
@Override
public void open(android.support.v4.app.Fragment fragment) {
    if (TextUtils.isEmpty(alias())) {
        super.open(fragment);
    } else {
        open(fragment.getActivity());
    }
}
 
Example #21
Source File: MainActivity.java    From QuickLyric with GNU General Public License v3.0 5 votes vote down vote up
private Fragment getDisplayedFragment(Fragment[] fragments) {
    for (Fragment fragment : fragments) {
        if (fragment == null)
            continue;
        if ((fragment instanceof LyricsViewFragment && ((LyricsViewFragment) fragment).isActiveFragment)
                || (fragment instanceof LocalLyricsFragment && ((LocalLyricsFragment) fragment).isActiveFragment)
                || (fragment instanceof RecentTracksFragment && ((RecentTracksFragment) fragment).isActiveFragment))
            return fragment;
    }
    return fragments[0];
}
 
Example #22
Source File: InstagramPhotoPicker.java    From InstagramPhotoPicker-Android with MIT License 5 votes vote down vote up
public static void startPhotoPickerForResult(Fragment fragment, String clientId, String redirectUri, int requestCode) {
    String accessToken = getAccessToken(fragment.getActivity());
    if (accessToken != null) {
        if (clientId.equals(cachedClientId)) {
            InstagramGalleryActivity.startForResult(fragment, requestCode);
            return;
        } else {
            logout(fragment.getActivity()); // clear cache & preferences if any.
        }
    }

    InstagramLoginActivity.startLoginForResult(fragment, clientId, redirectUri, requestCode);
}
 
Example #23
Source File: FaBoArduinoActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * サービスとアンバインドされたことを通知します.
 */
private void notifyUnbindService() {
    FragmentManager manager = getFragmentManager();
    Fragment f = manager.findFragmentById(R.id.fragment_container);
    if (f != null && f instanceof FaBoArduinoFragment) {
        ((FaBoArduinoFragment) f).onUnbindService();
    }
}
 
Example #24
Source File: DialtactsActivity.java    From coursera-android with MIT License 5 votes vote down vote up
private void sendFragmentVisibilityChange(int position, boolean visibility) {
    if (DEBUG) {
        Log.d(TAG, "sendFragmentVisibiltyChange(). position: " + position
                + ", visibility: " + visibility);
    }
    // Position can be -1 initially. See PageChangeListener.
    if (position >= 0) {
        final Fragment fragment = getFragmentAt(position);
        if (fragment != null) {
            fragment.setMenuVisibility(visibility);
            fragment.setUserVisibleHint(visibility);
        }
    }
}
 
Example #25
Source File: Crop.java    From XERUNG with Apache License 2.0 5 votes vote down vote up
/**
 * Pick image from a Fragment with a custom request code
 *
 * @param context     Context
 * @param fragment    Fragment to receive result
 * @param requestCode requestCode for result
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void pickImage(Context context, Fragment fragment, int requestCode) {
    try {
        fragment.startActivityForResult(getImagePicker(), requestCode);
    } catch (ActivityNotFoundException e) {
        showImagePickerError(context);
    }
}
 
Example #26
Source File: TrackPresenterImpl.java    From RunMap with Apache License 2.0 5 votes vote down vote up
@Override
public void showMapUiLayout(Context context, RelativeLayout mapUI, Fragment mapFrag) {
    if(isMapFragmentShowing){
        return;
    }
    mapFrag.setUserVisibleHint(true);
    Animation animation = AnimationUtils.loadAnimation(context, R.anim.scale_in);
    mapUI.startAnimation(animation);
    isMapFragmentShowing = true;
}
 
Example #27
Source File: MainActivity.java    From Paperwork-Android with MIT License 5 votes vote down vote up
@Override
public void updateView()
{
    NoteDataSource notesDataSource = NoteDataSource.getInstance(this);
    List<Tag> tags = notesDataSource.getAllTags();

    mTagMenu.clear();

    for (final Tag tag : tags)
    {
        final MenuItem menuItem = mTagMenu.add(tag.getTitle());
        menuItem.setIcon(R.drawable.ic_tag);

        menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem item)
            {
                setTitle(tag.getTitle());
                NotesFragment noteFragment = (NotesFragment) Fragment.instantiate(MainActivity.this, NotesFragment.class.getName());
                Bundle bundle = new Bundle();
                bundle.putSerializable(NotesFragment.KEY_TAG, tag);
                noteFragment.setArguments(bundle);
                (getFragmentManager().beginTransaction().replace(R.id.main_container, noteFragment)).commit();
                mDrawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });
    }
}
 
Example #28
Source File: ElementGridPagerAdapter.java    From WearViewStub with Apache License 2.0 5 votes vote down vote up
private Fragment createActionFragment() {
    return new Fragment(){
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_action,container,false);
        }
    };
}
 
Example #29
Source File: SugarTask.java    From SugarTask with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void registerHookToContext(@NonNull Fragment fragment) {
    FragmentManager manager = fragment.getChildFragmentManager();

    HookFragment hookFragment = (HookFragment) manager.findFragmentByTag(TAG_HOOK);
    if (hookFragment == null) {
        hookFragment = new HookFragment();
        manager.beginTransaction().add(hookFragment, TAG_HOOK).commitAllowingStateLoss();
    }
}
 
Example #30
Source File: FakeSignatureGlobalWarningDialog.java    From haystack with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onClick(DialogInterface dialog, int which) {
    if (which == DialogInterface.BUTTON_POSITIVE) {
        Fragment fragment = getTargetFragment();
        if (fragment != null) {
            ((DevelopmentSettingsDashboardFragment) fragment).onFakeSignatureGlobalDialogConfirmed();
        }
    }
}