Java Code Examples for android.support.v4.app.FragmentTransaction#commitNow()

The following examples show how to use android.support.v4.app.FragmentTransaction#commitNow() . 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: WebViewCarFragment.java    From carstream-android-auto with Apache License 2.0 6 votes vote down vote up
private void hideBookmarksScreen() {
    if (isAdded()) {
        if (!nativePlayerActive) {
            webView.setVisibility(View.VISIBLE);
            toolbar.setVisibility(View.VISIBLE);
            showToolbar();
        }
        FragmentManager childFragmentManager = getChildFragmentManager();
        Fragment oldFragment = childFragmentManager.findFragmentByTag(BOOKMARKS_FRAGMENT_TAG);
        if (oldFragment != null) {
            FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
            fragmentTransaction.remove(oldFragment);
            fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
            fragmentTransaction.commitNow();
        }
    }
    if (!nativePlayerActive) {
        webView.requestFocus();
    }
}
 
Example 2
Source File: MvcActivity.java    From AndroidMvc with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    eventRegister = new EventRegister(this);

    eventRegister.registerEventBuses();

    delegateFragment = (DelegateFragment) getSupportFragmentManager().findFragmentByTag(
            getDelegateFragmentTag());

    if (delegateFragment == null) {
        //Brand new container fragment
        try {
            delegateFragment = (DelegateFragment) new ReflectUtils.newObjectByType(
                    checkDelegateFragmentClass()).newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Failed to instantiate delegate fragment.", e);
        }
        FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
        trans.replace(android.R.id.content, delegateFragment, getDelegateFragmentTag());
        trans.commitNow();
    }
}
 
Example 3
Source File: FragmentContainerActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // Inflate the layout containing the Fragment containers
  setContentView(R.layout.fragment_container_layout);

  FragmentManager fragmentManager = getSupportFragmentManager();

  // Check to see if the Fragment containers have been populated
  // with Fragment instances. If not, create and populate the layout.
  DetailFragment detailsFragment =
    (DetailFragment) fragmentManager.findFragmentById(R.id.details_container);

  if (detailsFragment == null) {
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.add(R.id.details_container, new DetailFragment());
    ft.add(R.id.list_container, new MyListFragment());
    ft.commitNow();
  }
}
 
Example 4
Source File: WebViewCarFragment.java    From carstream-android-auto with Apache License 2.0 6 votes vote down vote up
private void showWarningScreen() {
    if (isAdded()) {
        warningScreenOpen = true;
        FragmentManager childFragmentManager = getChildFragmentManager();
        SafetyWarningFragment warningFragment = (SafetyWarningFragment) childFragmentManager.findFragmentByTag(SAFETY_WARNING_FRAGMENT_TAG);
        if (warningFragment == null) {
            warningFragment = new SafetyWarningFragment();
        }
        FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.overlay_container, warningFragment, SAFETY_WARNING_FRAGMENT_TAG);
        fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
        fragmentTransaction.commitNow();
        webView.setVisibility(View.GONE);
        toolbar.setVisibility(View.GONE);
    }

}
 
Example 5
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example 6
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example 7
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example 8
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example 9
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment)fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  Date now = Calendar.getInstance().getTime();
  List<Earthquake> dummyQuakes = new ArrayList<Earthquake>(0);
  dummyQuakes.add(new Earthquake("0", now, "San Jose", null, 7.3, null));
  dummyQuakes.add(new Earthquake("1", now, "LA", null, 6.5, null));
  mEarthquakeListFragment.setEarthquakes(dummyQuakes);
}
 
Example 10
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example 11
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example 12
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example 13
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment)fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  Date now = Calendar.getInstance().getTime();
  List<Earthquake> dummyQuakes = new ArrayList<Earthquake>(0);
  dummyQuakes.add(new Earthquake("0", now, "San Jose", null, 7.3, null));
  dummyQuakes.add(new Earthquake("1", now, "LA", null, 6.5, null));
  mEarthquakeListFragment.setEarthquakes(dummyQuakes);
}
 
Example 14
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment)fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  Date now = Calendar.getInstance().getTime();
  List<Earthquake> dummyQuakes = new ArrayList<Earthquake>(0);
  dummyQuakes.add(new Earthquake("0", now, "San Jose", null, 7.3, null));
  dummyQuakes.add(new Earthquake("1", now, "LA", null, 6.5, null));
  mEarthquakeListFragment.setEarthquakes(dummyQuakes);
}
 
Example 15
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

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

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example 16
Source File: CompatActivity.java    From NoFragment with Apache License 2.0 5 votes vote down vote up
/**
 * Show a fragment.
 *
 * @param thisFragment Now show fragment, can be null.
 * @param thatFragment fragment to display.
 * @param stickyStack  sticky back stack.
 * @param requestCode  requestCode.
 * @param <T>          {@link NoFragment}.
 */
protected final <T extends NoFragment> void startFragment(T thisFragment, T thatFragment,
                                                          boolean stickyStack, int requestCode) {
    FragmentTransaction fragmentTransaction = mFManager.beginTransaction();
    if (thisFragment != null) {
        FragmentStackEntity thisStackEntity = mFragmentEntityMap.get(thisFragment);
        if (thisStackEntity != null) {
            if (thisStackEntity.isSticky) {
                thisFragment.onPause();
                thisFragment.onStop();
                fragmentTransaction.hide(thisFragment);
            } else {
                fragmentTransaction.remove(thisFragment).commit();
                fragmentTransaction.commitNow();
                fragmentTransaction = mFManager.beginTransaction();

                mFragmentEntityMap.remove(thisFragment);
                mFragmentStack.remove(thisFragment);
            }
        }
    }

    String fragmentTag = thatFragment.getClass().getSimpleName() + mAtomicInteger.incrementAndGet();
    fragmentTransaction.add(fragmentLayoutId(), thatFragment, fragmentTag);
    fragmentTransaction.addToBackStack(fragmentTag);
    fragmentTransaction.commit();

    FragmentStackEntity fragmentStackEntity = new FragmentStackEntity();
    fragmentStackEntity.isSticky = stickyStack;
    fragmentStackEntity.requestCode = requestCode;
    thatFragment.setStackEntity(fragmentStackEntity);
    mFragmentEntityMap.put(thatFragment, fragmentStackEntity);

    mFragmentStack.add(thatFragment);
}
 
Example 17
Source File: MainActivity.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
/**
     * 修改显示的内容 不会重新加载
     **/
    public void switchContent(Fragment to) {
        Boolean isAdded = false;
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (getSupportFragmentManager().getFragments() != null) {
                for (Fragment f : getSupportFragmentManager().getFragments()) {
                    if (to.getClass().isAssignableFrom(f.getClass())) {
                        if (!f.isAdded()) {
                            transaction.add(R.id.fl_contain, f, f.getClass().getName());
                        } else {
                            transaction.show(f);
                        }
                        isAdded = true;
                    } else {
                        transaction.hide(f);
                        f.setUserVisibleHint(false);
                    }
                }
            }
            if (!isAdded) {
                if (!to.isAdded()) { // 先判断是否被add过
                    transaction.add(R.id.fl_contain, to, to.getClass().getName()).commitNow();
                } else {
                    transaction.show(to).commitNow(); // 隐藏当前的fragment,显示下一个
                }
            } else {
                transaction.commitNow();
            }
            if (getSupportFragmentManager().findFragmentByTag(to.getClass().getName()) != null) {
                getSupportFragmentManager().findFragmentByTag(to.getClass().getName()).setUserVisibleHint(true);
            }
//            setSelectedFragment((BaseFragment) to);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 18
Source File: IBaseActivity.java    From Collection-Android with MIT License 4 votes vote down vote up
/**
 * Show a fragment.
 *
 * @param thisFragment Now show fragment, can be null.
 * @param thatFragment fragment to display.
 * @param stickyStack  sticky back stack.
 * @param requestCode  requestCode.
</T> */
public  <T extends IBaseFragment> void startFragment(
		T thisFragment, T thatFragment,
		boolean stickyStack, int requestCode,
		boolean isSkipAnimation
) {
	FragmentTransaction fragmentTransaction = mFManager.beginTransaction();
	if (thisFragment != null) {
		FragmentStackEntity thisStackEntity = (FragmentStackEntity) mFragmentEntityMap.get(thisFragment);
		if (thisStackEntity != null) {
			if (thisStackEntity.isSticky) {
				thisFragment.onPause();
				thisFragment.onStop();
				fragmentTransaction.hide(thisFragment);
				if(isSkipAnimation){
					fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
				}

			} else {
				fragmentTransaction.remove(thisFragment).commit();
				if(isSkipAnimation){
					fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
				}
				fragmentTransaction.commitNow();
				fragmentTransaction = mFManager.beginTransaction();
				mFragmentEntityMap.remove(thisFragment);
				mFragmentStack.remove(thisFragment);
				fragmentStack.remove(thisFragment.getClass().getSimpleName());
			}
		}
	}
	String fragmentTag =
			thatFragment.getClass().getSimpleName();
	fragmentTransaction.add(fragmentLayoutId(), thatFragment, fragmentTag);
	if(isSkipAnimation){
		fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
	}
	fragmentTransaction.addToBackStack(fragmentTag);
	fragmentTransaction.commit();
	mFManager.executePendingTransactions();
	FragmentStackEntity fragmentStackEntity = new FragmentStackEntity();
	fragmentStackEntity.isSticky = stickyStack;
	fragmentStackEntity.requestCode = requestCode;
	thatFragment.setStackEntity(fragmentStackEntity);
	mFragmentEntityMap.put(thatFragment,fragmentStackEntity);
	mFragmentStack.add(thatFragment);
	fragmentStack.put(fragmentTag,thatFragment);
}
 
Example 19
Source File: MainActivity.java    From Flubber with Apache License 2.0 3 votes vote down vote up
private void openEditor(CustomAnimationBody animationBody) {

        final EditorFragment editorFragment = new EditorFragment();
        final FragmentManager fragmentManager = getSupportFragmentManager();

        editorFragment.setAnimationBody(animationBody);

        final FragmentTransaction transaction =
                fragmentManager.beginTransaction()
                        .replace(R.id.editorPanelContainer, editorFragment, EditorFragment.TAG);

        binding.mainPanelContainer.setVisibility(View.INVISIBLE);

        final Animator showAnimation = getShowWithReveal(binding.editorPanelContainer);

        Flubber.with()
                .animation(FABRevealProvider.create(R.drawable.ic_done_white_24dp))
                .duration(DURATION_REVEAL)
                .autoStart(true)
                .createFor(binding.floatingActionButton);

        showAnimation.start();

        transaction.commitNow();

        binding.setEditorOpen(true);
    }