Java Code Examples for androidx.fragment.app.FragmentManager#findFragmentById()

The following examples show how to use androidx.fragment.app.FragmentManager#findFragmentById() . 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: ArticleListActivity.java    From NGA-CLIENT-VER-OPEN-SOURCE with GNU General Public License v2.0 6 votes vote down vote up
private void setupFragment() {
    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(android.R.id.content);

    if (fragment == null) {
        if (mRequestParam.searchPost == 0) {
            fragment = new ArticleTabFragment();
        } else {
            fragment = new ArticleSearchFragment();
        }
        fragment.setHasOptionsMenu(true);
        Bundle bundle = new Bundle();
        bundle.putParcelable(ParamKey.KEY_PARAM, mRequestParam);
        fragment.setArguments(bundle);
        fm.beginTransaction().replace(android.R.id.content, fragment).commit();
    } else {
        fragment.setHasOptionsMenu(true);
    }
}
 
Example 2
Source File: UserInfoActivity.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_user_info);
    mRootView = findViewById(R.id.main_activity_root_view);
    mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);

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

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

    FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager.findFragmentById(R.id.main_frame_container) == null) {
        Bundle arguments = getIntent().getExtras();
        UserinfoFragment fragment = new UserinfoFragment();
        fragment.setArguments(arguments);
        fragmentManager.beginTransaction()
                .add(R.id.main_frame_container, fragment).commit();
    }
}
 
Example 3
Source File: SettingsAppearanceActivity.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_settings_appearance);
       if (savedInstanceState == null) {
           FragmentManager fm = getSupportFragmentManager();
           mSettingsFragment = (AppearanceSettingsFragment) fm.findFragmentById(R.id.appearance_fragment);

           if (mSettingsFragment == null) {
               mSettingsFragment = AppearanceSettingsFragment.newInstance();
           }
       }

       Toolbar mToolbar = (Toolbar) findViewById(R.id.settings_appearance_toolbar);
       setSupportActionBar(mToolbar);
       if (getSupportActionBar() != null) {
           getSupportActionBar().setDisplayHomeAsUpEnabled(true);
       }
}
 
Example 4
Source File: FragmentHelper.java    From AndroidNavigation with MIT License 6 votes vote down vote up
public static void addFragmentToBackStack(@NonNull FragmentManager fragmentManager, int containerId, @NonNull AwesomeFragment fragment, @NonNull PresentAnimation animation) {
    if (fragmentManager.isDestroyed()) {
        return;
    }

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setReorderingAllowed(true);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    AwesomeFragment topFragment = (AwesomeFragment) fragmentManager.findFragmentById(containerId);
    if (topFragment != null && topFragment.isAdded()) {
        topFragment.setAnimation(animation);
        transaction.setMaxLifecycle(topFragment, Lifecycle.State.STARTED);
        transaction.hide(topFragment);
    }
    fragment.setAnimation(animation);

    transaction.add(containerId, fragment, fragment.getSceneId());
    transaction.addToBackStack(fragment.getSceneId());
    transaction.commit();
    executePendingTransactionsSafe(fragmentManager);
}
 
Example 5
Source File: FragmentHelper.java    From AndroidNavigation with MIT License 6 votes vote down vote up
public static void handleDismissFragment(@NonNull AwesomeFragment target, @NonNull AwesomeFragment presented, @Nullable AwesomeFragment top) {
    FragmentManager fragmentManager = target.requireFragmentManager();
    target.setAnimation(PresentAnimation.Modal);

    if (top == null) {
        top = (AwesomeFragment) fragmentManager.findFragmentById(target.getContainerId());
    }

    if (top == null) {
        return;
    }

    top.setAnimation(PresentAnimation.Modal);
    fragmentManager.beginTransaction().setMaxLifecycle(presented, Lifecycle.State.STARTED).commit();
    fragmentManager.popBackStack(presented.getSceneId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
    FragmentHelper.executePendingTransactionsSafe(fragmentManager);
    target.onFragmentResult(top.getRequestCode(), top.getResultCode(), top.getResultData());
}
 
Example 6
Source File: ImageFragment.java    From mcumgr-android with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("ConstantConditions")
@Override
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final FragmentManager fm = getChildFragmentManager();
    mImageUpgradeFragment = fm.findFragmentById(R.id.fragment_image_upgrade);
    mImageUploadFragment = fm.findFragmentById(R.id.fragment_image_upload);
    mImageControlFragment = fm.findFragmentById(R.id.fragment_image_control);
    mResetFragment = fm.findFragmentById(R.id.fragment_reset);

    // Initially, show only the basic Image Upgrade fragment
    if (savedInstanceState == null) {
        getChildFragmentManager().beginTransaction()
                .hide(mImageUploadFragment)
                .hide(mImageControlFragment)
                .hide(mResetFragment)
                .commit();
    }

    mViewModel.getBusyState().observe(this, busy -> {
        mOperationInProgress = busy;
        requireActivity().invalidateOptionsMenu();
    });
}
 
Example 7
Source File: MainFrameActivity.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onBackPressed() {
    if (mDrawer.isDrawerOpen()) {
        mDrawer.closeDrawer();
        return;
    }

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.main_frame_container);

    if (fragment instanceof BaseFragment) {
        if (((BaseFragment) fragment).onBackPressed())
            return;
    }

    finishWithDefault();
}
 
Example 8
Source File: PostActivity.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_post);
    mRootView = findViewById(R.id.main_activity_root_view);
    mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);

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

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

    FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager.findFragmentById(R.id.main_frame_container) == null) {
        Bundle arguments = getIntent().getExtras();
        PostFragment fragment = new PostFragment();
        fragment.setArguments(arguments);
        fragmentManager.beginTransaction()
                .add(R.id.main_frame_container, fragment).commit();
    }

    setSwipeBackEnable(false);
}
 
Example 9
Source File: TopicListActivity.java    From NGA-CLIENT-VER-OPEN-SOURCE with GNU General Public License v2.0 6 votes vote down vote up
private void setupFragment() {
    FragmentManager fm = getSupportFragmentManager();
    if (fm.findFragmentById(android.R.id.content) == null) {
        Fragment fragment;
        if (mRequestParam.favor != 0) {
            fragment = new TopicFavoriteFragment();
        } else if (isBoardTopicList()) {
            fragment = new TopicListFragment();
        } else {
            fragment = new TopicSearchFragment();
        }
        Bundle bundle = new Bundle();
        bundle.putParcelable(ParamKey.KEY_PARAM, mRequestParam);
        fragment.setArguments(bundle);
        fm.beginTransaction().replace(android.R.id.content, fragment).commit();
    }
}
 
Example 10
Source File: AirMapView.java    From AirMapView with Apache License 2.0 5 votes vote down vote up
/**
 * Used for initialization of the underlying map provider.
 *
 * @param fragmentManager required for initialization
 */
public void initialize(FragmentManager fragmentManager) {
  AirMapInterface mapInterface = (AirMapInterface)
      fragmentManager.findFragmentById(R.id.map_frame);

  if (mapInterface != null) {
    initialize(fragmentManager, mapInterface);
  } else {
    initialize(fragmentManager, new DefaultAirMapViewBuilder(getContext()).builder().build());
  }
}
 
Example 11
Source File: HostActivity.java    From AndroidQuick with MIT License 5 votes vote down vote up
@Override
protected void initViewsAndEvents(Bundle savedInstanceState) {
    FragmentManager fm = getSupportFragmentManager();
    mContentFragment = fm.findFragmentById(R.id.content_frame);

    if(mContentFragment == null )
    {
        mContentFragment = ExampleFragment.newInstance("");
        fm.beginTransaction().add(R.id.content_frame,mContentFragment).commit();
    }
}
 
Example 12
Source File: FragmentsSelectTest.java    From itag with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void bleTurnedOn3times_shouldReturnITagsFragment() {
    mockBluetoothAdapter =  Mockito.mock(BluetoothAdapter.class);
    when(mockBluetoothAdapter.isEnabled())
            .thenReturn(false) // disabled
            .thenReturn(false) // turned on 1
            .thenReturn(false) // turned on 2
            .thenReturn(true); // enabled
    when(mockBluetoothManager.getAdapter()).thenReturn(mockBluetoothAdapter);
    MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
    final FragmentManager fragmentManager = mainActivity.getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.content);
    Assert.assertTrue(fragment instanceof ITagsFragment);
}
 
Example 13
Source File: AddCameraActivity.java    From evercam-android with GNU Affero General Public License v3.0 5 votes vote down vote up
private void initModelSelectorUI() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    mModelSelectorFragment = (ModelSelectorFragment)
            fragmentManager.findFragmentById(R.id.add_camera_model_selector_fragment);
    mModelSelectorFragment.hideModelQuestionMark();
    Button modelSelectorViewNextButton = (Button) findViewById(R.id.model_selector_view_next_button);
    TextView reportModelLink = (TextView) findViewById(R.id.report_model_text_link);

    reportModelLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CustomedDialog.showReportCameraModelDialog(AddCameraActivity.this, null);
        }
    });

    modelSelectorViewNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String modelId = mModelSelectorFragment.getModelIdFromSpinner();
            String modelName = mModelSelectorFragment.getModelNameFromSpinner();
            String vendorId = mModelSelectorFragment.getVendorIdFromSpinner();
            String vendorName = mModelSelectorFragment.getVendorNameFromSpinner();
            mSelectedModel = new SelectedModel(modelId, modelName, vendorId, vendorName, mSelectedModelDefaults);

            showConnectCameraView();
        }
    });
}
 
Example 14
Source File: FragmentsSelectTest.java    From itag with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void bleDisabled_shouldReturnDisabledBLEFragment() {
    mockBluetoothAdapter =  Mockito.mock(BluetoothAdapter.class);
    when(mockBluetoothAdapter.isEnabled()).thenReturn(false);
    when(mockBluetoothManager.getAdapter()).thenReturn(mockBluetoothAdapter);
    MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
    final FragmentManager fragmentManager = mainActivity.getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.content);
    Assert.assertTrue(fragment instanceof DisabledBLEFragment);
}
 
Example 15
Source File: FixesTest.java    From itag with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void issue38_setupLeScanFragment() {
    mainActivity.onStartStopScan(null);
    final FragmentManager fragmentManager = mainActivity.getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.content);
    Assert.assertTrue(fragment instanceof ScanFragment);
}
 
Example 16
Source File: AwesomeActivity.java    From AndroidNavigation with MIT License 5 votes vote down vote up
protected void showDialogInternal(AwesomeFragment dialog, int requestCode) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager.getBackStackEntryCount() > 0) {
        Fragment fragment = fragmentManager.findFragmentById(android.R.id.content);
        if (fragment != null && fragment.isAdded()) {
            dialog.setTargetFragment(fragment, requestCode);
        }
    }
    Bundle args = FragmentHelper.getArguments(dialog);
    args.putBoolean(AwesomeFragment.ARGS_SHOW_AS_DIALOG, true);
    dialog.show(fragmentManager, dialog.getSceneId());
    FragmentHelper.executePendingTransactionsSafe(fragmentManager);
}
 
Example 17
Source File: Installer.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
private void setOnMainActivityChangeListener() {
    FragmentManager manager = mainActivity.getSupportFragmentManager();
    TopFragment topFragment = (TopFragment) manager.findFragmentById(R.id.Topfrg);
    if (topFragment != null) {
        topFragment.setOnActivityChangeListener(this);
    }
}
 
Example 18
Source File: DashboardActivity.java    From arcusandroid with Apache License 2.0 4 votes vote down vote up
@Override
public void onBackPressed() {

    if (mNavigationDrawerFragment.isDrawerOpen()){
        mNavigationDrawerFragment.closeDrawer();
        return;
    }
    boolean eventConsumed = false;
    Fragment fragment = BackstackManager.getInstance().getCurrentFragment();

    if (isBaseFragment(fragment)) {
        eventConsumed = ((BaseFragment) fragment).onBackPressed();
    } else if (fragment instanceof NoViewModelFragment) {
        eventConsumed = ((NoViewModelFragment) fragment).onBackPressed();
    }

    if (!eventConsumed) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragment = fragmentManager.findFragmentById(R.id.floating);
        if (isBaseFragment(fragment)) {
            eventConsumed = ((BaseFragment) fragment).onBackPressed();
        }

        if (!eventConsumed) {
            int backStackEntryCount = fragmentManager.getBackStackEntryCount();
            if (backStackEntryCount > 1) {
                BackstackManager.getInstance().navigateBack();
            } else if (backStackEntryCount == 1) {
                fragment = fragmentManager.findFragmentById(R.id.container);
                if (isBaseFragment(fragment) && ((BaseFragment) fragment).isHomeFragment()) {
                    super.onBackPressed();
                } else {
                    BackstackManager.getInstance().navigateBack();
                }
            } else {
                super.onBackPressed();
            }
        }
    } else {
        logger.trace("back press event consumed", "");
    }
}
 
Example 19
Source File: SDK21IntTest.java    From itag with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void reproduceResourceNotFound() {
    final FragmentManager fragmentManager = mainActivity.getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentById(R.id.content);
    Assert.assertTrue(fragment instanceof ITagsFragment);
}
 
Example 20
Source File: OdysseyMainActivity.java    From odyssey with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    final FragmentManager fragmentManager = getSupportFragmentManager();

    if (item.getItemId() == android.R.id.home) {
        if (fragmentManager.findFragmentById(R.id.fragment_container) instanceof FilesFragment) {
            // handle click events for the files fragment manually

            final FilesFragment fragment = (FilesFragment) fragmentManager.findFragmentById(R.id.fragment_container);

            if (fragment.isRootDirectory()) {
                // current directory is a root directory so enable navigation drawer

                mDrawerToggle.setDrawerIndicatorEnabled(true);

                if (mDrawerToggle.onOptionsItemSelected(item)) {
                    return true;
                }
            } else {
                if (fragmentManager.getBackStackEntryCount() == 0) {
                    // if backstack is empty but root directory not reached create an new fragment with the parent directory
                    final List<String> storageVolumesList = mFileExplorerHelper.getStorageVolumes(getApplicationContext());

                    final String parentDirectoryPath = fragment.getCurrentDirectory().getParent();

                    // don't add this this directory to the backstack
                    onDirectorySelected(parentDirectoryPath, storageVolumesList.contains(parentDirectoryPath), false);
                } else {
                    // back stack not empty so just use the standard back press mechanism
                    onBackPressed();
                }
            }

        } else if (fragmentManager.getBackStackEntryCount() > 0) {
            onBackPressed();
        } else {
            // back stack empty so enable navigation drawer

            mDrawerToggle.setDrawerIndicatorEnabled(true);

            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
        }
    }

    return super.onOptionsItemSelected(item);
}