androidx.fragment.app.FragmentManager.beginTransaction()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(111)

本文整理了Java中androidx.fragment.app.FragmentManager.beginTransaction()方法的一些代码示例,展示了FragmentManager.beginTransaction()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FragmentManager.beginTransaction()方法的具体详情如下:
包路径:androidx.fragment.app.FragmentManager
类名称:FragmentManager
方法名:beginTransaction

FragmentManager.beginTransaction介绍

暂无

代码示例

代码示例来源:origin: ankidroid/Anki-Android

private void loadStudyOptionsFragment() {
  boolean withDeckOptions = false;
  if (getIntent().getExtras() != null) {
    withDeckOptions = getIntent().getExtras().getBoolean("withDeckOptions");
  }
  StudyOptionsFragment currentFragment = StudyOptionsFragment.newInstance(withDeckOptions);
  getSupportFragmentManager().beginTransaction().replace(R.id.studyoptions_frame, currentFragment).commit();
}

代码示例来源:origin: square/picasso

void showDetails(String url) {
 getSupportFragmentManager().beginTransaction()
   .replace(R.id.sample_content, DetailFragment.newInstance(url))
   .addToBackStack(null)
   .commit();
}

代码示例来源:origin: square/picasso

@Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 if (savedInstanceState == null) {
  getSupportFragmentManager().beginTransaction()
    .add(R.id.sample_content, ListFragment.newInstance())
    .commit();
 }
}

代码示例来源:origin: ArthurHub/Android-Image-Cropper

private void setMainFragmentByPreset(CropDemoPreset demoPreset) {
 FragmentManager fragmentManager = getSupportFragmentManager();
 fragmentManager
   .beginTransaction()
   .replace(R.id.container, MainFragment.newInstance(demoPreset))
   .commit();
}

代码示例来源:origin: andkulikov/Transitions-Everywhere

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ListFragment listFragment = new ListFragment();
  listFragment.setSampleListListener(this);
  getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.container, listFragment)
    .commit();
}

代码示例来源:origin: andkulikov/Transitions-Everywhere

@Override
public void onSampleSelected(int index) {
  getSupportFragmentManager()
    .beginTransaction()
    .setCustomAnimations(R.anim.fade_in, R.anim.fade_out,
      R.anim.fade_in, R.anim.fade_out)
    .replace(R.id.container, createFragmentForPosition(index))
    .addToBackStack(String.valueOf(index))
    .commit();
}

代码示例来源:origin: mikepenz/MaterialDrawer

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  //handle the click on the back arrow click
  switch (item.getItemId()) {
    case R.id.menu_1:
      Fragment f = DrawerFragment.newInstance("Demo");
      getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();
      return true;
    case R.id.menu_2:
      Fragment f2 = SecondDrawerFragment.newInstance("Demo 2");
      getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f2).commit();
      return true;
    case android.R.id.home:
      onBackPressed();
      return true;
    default:
      return super.onOptionsItemSelected(item);
  }
}

代码示例来源:origin: ankidroid/Anki-Android

/**
 * Load a new studyOptionsFragment. If withDeckOptions is true, the deck options activity will
 * be loaded on top of it. Use this flag when creating a new filtered deck to allow the user to
 * modify the filter settings before being shown the fragment. The fragment itself will handle
 * rebuilding the deck if the settings change.
 */
private void loadStudyOptionsFragment(boolean withDeckOptions) {
  StudyOptionsFragment details = StudyOptionsFragment.newInstance(withDeckOptions);
  FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  ft.replace(R.id.studyoptions_fragment, details);
  ft.commit();
}

代码示例来源:origin: guardianproject/haven

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_settings);
  if (savedInstanceState == null) {
    mFragment = new SettingsFragment();
    getSupportFragmentManager().beginTransaction()
        .add(R.id.settings_fragment, mFragment)
        .commit();
  }
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

代码示例来源:origin: niorgai/StatusBarCompat

private void changeTab(int page) {
  if (currentTab == page) {
    return;
  }
  Fragment fragment = fragments.get(page);
  FragmentTransaction ft = MainActivity.this.getSupportFragmentManager().beginTransaction();
  if(!fragment.isAdded()){
    ft.add(fragmentContentId,fragment);
  }
  ft.hide(fragments.get(currentTab));
  ft.show(fragments.get(page));
  changeButtonStatus(currentTab, false);
  currentTab = page;
  changeButtonStatus(currentTab, true);
  if (!this.isFinishing()) {
    ft.commitAllowingStateLoss();
  }
}

代码示例来源:origin: mikepenz/MaterialDrawer

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_sample_fragment_dark_toolbar);
  // Handle Toolbar
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);
  getSupportActionBar().setTitle(R.string.drawer_item_fragment_drawer);
  //ignore the DemoFragment and it's layout it's just to showcase the handle with an keyboard
  if (savedInstanceState == null) {
    Fragment f = DrawerFragment.newInstance("Demo");
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();
  }
}

代码示例来源:origin: cSploit/android

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  SharedPreferences themePrefs = getSharedPreferences("THEME", 0);
  if (themePrefs.getBoolean("isDark", false))
    setTheme(R.style.DarkTheme);
  else
    setTheme(R.style.AppTheme);
  setContentView(R.layout.main);
  if (findViewById(R.id.mainframe) != null) {
    if (savedInstanceState != null) {
      return;
    }
    f = new SettingsFragment();
    getSupportFragmentManager().beginTransaction()
        .add(R.id.mainframe, f).commit();
  }
}

代码示例来源:origin: cSploit/android

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 SharedPreferences themePrefs = getSharedPreferences("THEME", 0);
 if (themePrefs.getBoolean("isDark", false))
  setTheme(R.style.DarkTheme);
 else
  setTheme(R.style.AppTheme);
 setContentView(R.layout.main);
 if (findViewById(R.id.mainframe) != null) {
  if (savedInstanceState != null) {
   return;
  }
  f = new ActionFragment();
  getSupportFragmentManager().beginTransaction()
      .add(R.id.mainframe, f).commit();
 }
}

代码示例来源:origin: mikepenz/Android-Iconics

private void loadIcons(String fontName) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    mIconsFragment = IconsFragment.newInstance(fontName);
    mIconsFragment.randomize(mRandomize);
    mIconsFragment.shadow(mShadow);
    mIconsFragment.onSearch(mCurrentSearch);
    ft.replace(R.id.content, mIconsFragment);
    ft.commit();
  }
}

代码示例来源:origin: trello/RxLifecycle

private void startFragment(Fragment fragment) {
  Robolectric.setupActivity(FragmentActivity.class).getSupportFragmentManager()
    .beginTransaction()
    .add(fragment, null)
    .commit();
}

代码示例来源:origin: trello/RxLifecycle

private void startFragment(Fragment fragment) {
  Robolectric.setupActivity(FragmentActivity.class).getSupportFragmentManager()
    .beginTransaction()
    .add(fragment, null)
    .commit();
}

代码示例来源:origin: cSploit/android

@SuppressWarnings("ConstantConditions")
@Override
public void onCreate(Bundle savedInstanceState) {
  SharedPreferences themePrefs = getActivity().getSharedPreferences("THEME", 0);
  if (themePrefs.getBoolean("isDark", false))
    getActivity().setTheme(R.style.PrefsThemeDark);
  else
    getActivity().setTheme(R.style.PrefsTheme);
  super.onCreate(savedInstanceState);
  getActivity().getSupportFragmentManager().beginTransaction()
      .replace(android.R.id.content, new PrefsFrag())
      .commit();
}

代码示例来源:origin: trello/RxLifecycle

private ActivityController<FragmentActivity> startFragment(Fragment fragment) {
    ActivityController<FragmentActivity> controller = Robolectric.buildActivity(FragmentActivity.class);
    controller.create();
    controller.get().getSupportFragmentManager()
        .beginTransaction()
        .add(android.R.id.content, fragment)
        .commitNow();
    return controller;
  }
}

代码示例来源:origin: AppIntro/AppIntro

@Override
  public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
    if (drawerItem != null) {
      Fragment fragment = null;
      FragmentManager fragmentManager = getSupportFragmentManager();
      switch ((int) drawerItem.getIdentifier()) {
        case 1:
          fragment = new MainTabsFragment();
          break;
        case 2:
          fragment = new PermissionTabsFragment();
          break;
      }
      if (fragment != null) {
        fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
      }
      if (drawerItem instanceof Nameable) {
        setTitle(((Nameable) drawerItem).getName().getText(getApplicationContext()));
      }
    }
    return false;
  }
})

代码示例来源:origin: robolectric/robolectric

@Test
public void recreate_retainFragmentHostingActivity() {
 Fragment fragment = new Fragment();
 fragment.setRetainInstance(true);
 ActivityScenario<LifecycleOwnerActivity> activityScenario =
   ActivityScenario.launch(LifecycleOwnerActivity.class);
 assertThat(activityScenario).isNotNull();
 activityScenario.onActivity(
   activity -> {
    activity
      .getSupportFragmentManager()
      .beginTransaction()
      .add(android.R.id.content, fragment)
      .commitNow();
    assertThat(activity.getSupportFragmentManager().findFragmentById(android.R.id.content))
      .isSameAs(fragment);
   });
 activityScenario.recreate();
 activityScenario.onActivity(
   activity ->
     assertThat(activity.getSupportFragmentManager().findFragmentById(android.R.id.content))
       .isSameAs(fragment));
}

相关文章