@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (areColorTransitionsEnabled) { if (position < mPagerAdapter.getCount() - 1) { if (mPagerAdapter.getItem(position) instanceof ISlideBackgroundColorHolder && mPagerAdapter.getItem(position + 1) instanceof ISlideBackgroundColorHolder) { Fragment currentSlide = mPagerAdapter.getItem(position); Fragment nextSlide = mPagerAdapter.getItem(position + 1); ISlideBackgroundColorHolder currentSlideCasted = (ISlideBackgroundColorHolder) currentSlide; ISlideBackgroundColorHolder nextSlideCasted = (ISlideBackgroundColorHolder) nextSlide; // Check if both fragments are attached to an activity, // otherwise getDefaultBackgroundColor may fail. if (currentSlide.isAdded() && nextSlide.isAdded()) { int newColor = (int) argbEvaluator.evaluate( positionOffset, currentSlideCasted.getDefaultBackgroundColor(), nextSlideCasted.getDefaultBackgroundColor()); currentSlideCasted.setBackgroundColor(newColor); nextSlideCasted.setBackgroundColor(newColor); } } else { throw new IllegalStateException( "Color transitions are only available if all slides implement ISlideBackgroundColorHolder."); } } } }
protected void processAuth(@Size(2) String[] auth, OsmOAuth.AuthType type, String username) { if (auth == null) { if (mFragment.isAdded()) { new AlertDialog.Builder(mFragment.getActivity()) .setTitle(R.string.editor_login_error_dialog) .setPositiveButton(android.R.string.ok, null) .show(); Statistics.INSTANCE.trackEvent( Statistics.EventName.EDITOR_AUTH_REQUEST_RESULT, Statistics.params() .add(Statistics.EventParam.IS_SUCCESS, false) .add(Statistics.EventParam.TYPE, type.name)); } return; } OsmOAuth.setAuthorization(auth[0], auth[1], username); if (mFragment.isAdded()) Utils.navigateToParent(mFragment.getActivity()); Statistics.INSTANCE.trackEvent( Statistics.EventName.EDITOR_AUTH_REQUEST_RESULT, Statistics.params() .add(Statistics.EventParam.IS_SUCCESS, true) .add(Statistics.EventParam.TYPE, type.name)); }
@Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { for (int i = 0; i < rgs.getChildCount(); i++) { if (rgs.getChildAt(i).getId() == checkedId) { Fragment fragment = fragments.get(i); FragmentTransaction ft = obtainFragmentTransaction(i); getCurrentFragment().onPause(); // 暂停当前tab // getCurrentFragment().onStop(); // 暂停当前tab if (fragment.isAdded()) { // fragment.onStart(); // 启动目标tab的onStart() fragment.onResume(); // 启动目标tab的onResume() } else { ft.add(fragmentContentId, fragment); } showTab(i); // 显示目标tab ft.commit(); // 如果设置了切换tab额外功能功能接口 if (null != onRgsExtraCheckedChangedListener) { onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(radioGroup, checkedId, i); } } } }
// simple convenience function for when a fragment wants to log a URL // don't need to worry about making sure it only happens once, because Fragments manage their own // state // in a way that activities do not. public static void track(Fragment fragment, String url) { if (fragment.isAdded()) { GoogleAnalyticsTracker tracker = start(fragment.getActivity()); page(fragment.getActivity(), tracker, url); stop(tracker); } }
/** * 切换fragment * * @param to */ public void switchFragment(Fragment to) { // FragmentManager fragmentManager = getFragmentManager(); // FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // if (!to.isAdded()) { // 先判断是否被add过 // fragmentTransaction.hide(curFragment).add(R.id.frameLayout_main, to); // // 隐藏当前的fragment,add下一个到Activity中 // } else { // fragmentTransaction.hide(curFragment).show(to); // 隐藏当前的fragment,显示下一个 // } // fragmentTransaction.commitAllowingStateLoss(); if (!to.isAdded()) { // 先判断是否被add过 getSupportFragmentManager() .beginTransaction() .hide(curFragment) .add(R.id.frameLayout_main, to) .commitAllowingStateLoss(); // 隐藏当前的fragment,add下一个到Activity中 } else { getSupportFragmentManager() .beginTransaction() .hide(curFragment) .show(to) .commitAllowingStateLoss(); // 隐藏当前的fragment,显示下一个 } }
private void setFragment(Fragment fragment) { if (fragments == null) { return; } FragmentManager fragmentManager = this.getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); if (fragment.isAdded() && fragment.isVisible()) { return; } if (fragment.isAdded()) { transaction.show(fragment); } else { transaction.replace(R.id.content, fragment); } transaction.commit(); }
/** * 隐藏显示相应的frag,并将设置当前的fragment。 * * @param showFrag 将要显示的frag * @param hideFrag 要隐藏的frag。 */ private void changeFrag(Fragment showFrag, Fragment hideFrag) { if (showFrag == hideFrag) { LogCat.e(MainActivity.class.getName() + ":changeFrag:showFrag=hideFragment"); return; } FragmentTransaction ft = mFM.beginTransaction(); if (showFrag.isAdded()) { ft.show(showFrag); } else { ft.add(R.id.main_content_fl, showFrag, null); } if (hideFrag != null && hideFrag.isAdded()) { ft.hide(hideFrag); } ft.commitAllowingStateLoss(); mCurFragment = showFrag; }
/** * 设置fragment的跳转 * * @param back 是否允许返回 */ public static void setFragment(Fragment fragment, Boolean back) { FragmentTransaction transaction = mFragManager.beginTransaction(); // 防止快速切换导致崩溃 if (!fragment.isAdded()) { transaction.replace(R.id.fragment_container, fragment); } if (back) { transaction.addToBackStack(null); } transaction.commit(); }
private void attachFragment(Fragment fragment, String tag) { if (fragment != null) { if (fragment.isDetached()) { ensureTransaction(); fragmentTransaction.attach(fragment); } else if (!fragment.isAdded()) { ensureTransaction(); fragmentTransaction.add(containerId, fragment, tag); } } }
private void initFragments() { Fragment friend = getFriendsTimeLineFragment(); Fragment mentions = getMentionsTimeLineFragment(); Fragment comments = getCommentsTimeLineFragment(); Fragment fav = getFavFragment(); Fragment myself = getMyProfileFragment(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); if (!friend.isAdded()) { fragmentTransaction.add(R.id.menu_right_fl, friend, FriendsTimeLineFragment.class.getName()); fragmentTransaction.hide(friend); } if (!mentions.isAdded()) { fragmentTransaction.add(R.id.menu_right_fl, mentions, MentionsTimeLine.class.getName()); fragmentTransaction.hide(mentions); } if (!comments.isAdded()) { fragmentTransaction.add(R.id.menu_right_fl, comments, CommentsTimeLine.class.getName()); fragmentTransaction.hide(comments); } if (!fav.isAdded()) { fragmentTransaction.add(R.id.menu_right_fl, fav, MyFavListFragment.class.getName()); fragmentTransaction.hide(fav); } if (!myself.isAdded()) { fragmentTransaction.add(R.id.menu_right_fl, myself, UserInfoFragment.class.getName()); fragmentTransaction.hide(myself); } if (GlobalContext.getInstance().getAccountBean().isBlack_magic()) { Fragment search = getSearchFragment(); Fragment dm = getDMFragment(); if (!search.isAdded()) { fragmentTransaction.add( R.id.menu_right_fl, search, SearchMainParentFragment.class.getName()); fragmentTransaction.hide(search); } if (!dm.isAdded()) { fragmentTransaction.add(R.id.menu_right_fl, dm, DMUserListFragment.class.getName()); fragmentTransaction.hide(dm); } } if (!fragmentTransaction.isEmpty()) { fragmentTransaction.commit(); getSupportFragmentManager().executePendingTransactions(); } }
/** 改变Tab键,进行更换页面 */ public void onCheckedChanged(RadioGroup arg0, int arg1) { for (int i = 0; i < rGroup.getChildCount(); i++) { if (rGroup.getChildAt(i).getId() == arg1) { Fragment fragment = fragments.get(i); FragmentTransaction ft = obtainFragmentTransaction(i); getCurrentFragment().onPause(); // 暂停当前页面 if (fragment.isAdded()) { fragment.onResume(); // 启动目标tab的onResume() } else { ft.add(fgContentId, fragment); } showFragment(i); // 显示目标tab ft.commit(); } } }
public static void addFragment( FragmentTransaction ft, Fragment fragment, boolean addToBackStack, final int transition) { AppMain.isShelf = false; try { if (!fragment.isAdded()) { ft.replace(R.id.realtabcontent, fragment); ft.setTransition(transition); ft.addToBackStack(null); ft.commit(); } else { return; } } catch (Exception e) { // TODO: handle exception } }
@Override protected void onPostExecute(Boolean b) { super.onPostExecute(b); if (progress != null) { progress.dismiss(); } if (!b) { Toast.makeText(mContext, R.string.photo_upload_failed, Toast.LENGTH_LONG).show(); } else { Toast.makeText(mContext, R.string.photo_upload_successfully, Toast.LENGTH_LONG).show(); if (mFragment != null && mFragment.isAdded() && mCursor != null && mCursor.getCount() > 0) { ((POIDetailFragment) mFragment).reloadData(); } } }
@Override public Object instantiateItem(ViewGroup container, int position) { Fragment fragment = fragments.get(position); if (!fragment.isAdded()) { // 如果fragment还没有added FragmentTransaction ft = fragmentManager.beginTransaction(); ft.add(fragment, fragment.getClass().getSimpleName()); ft.commit(); /** * 在用FragmentTransaction.commit()方法提交FragmentTransaction对象后 会在进程的主线程中,用异步的方式来执行。 * 如果想要立即执行这个等待中的操作,就要调用这个方法(只能在主线程中调用)。 要注意的是,所有的回调和相关的行为都会在这个调用中被执行完成,因此要仔细确认这个方法的调用位置。 */ fragmentManager.executePendingTransactions(); } if (fragment.getView().getParent() == null) { container.addView(fragment.getView()); // 为viewpager增加布局 } return fragment.getView(); }
@Override public Parcelable saveState() { Bundle state = null; // 目前顯示的 Fragments for (int i = 0; i < mFragments.size(); i++) { Fragment f = mFragments.get(i); if (f != null && f.isAdded()) { if (state == null) { state = new Bundle(); } String key = "f" + i; mFragmentManager.putFragment(state, key, f); } } if (mFragmentStates.size() > 0) { if (state == null) { state = new Bundle(); } // Create new instance for some reason ArrayList<FragmentState> fs = new ArrayList<>(); fs.addAll(mFragmentStates); state.putParcelableArrayList("states_fragment", fs); ArrayList<Bundle> args = new ArrayList<>(); args.addAll(mFragmentArgs); state.putParcelableArrayList("arg_fragment", args); ArrayList<String> tagArray = new ArrayList<>(); tagArray.addAll(mFragmentTags); state.putStringArrayList("tag_fragment", tagArray); ArrayList<String> classNameArray = new ArrayList<>(); classNameArray.addAll(mFragmentClassNames); state.putStringArrayList("classname_fragment", classNameArray); } return state; }
public boolean isAdded() { return zzadh.isAdded(); }
protected Fragment getMainFragment() { Fragment fragment = getSupportFragmentManager().findFragmentById(getMainFragmentContainerId()); if (fragment != null && fragment.isAdded()) return fragment; return null; }
private void removeFragment(Fragment fragment) { if (fragment != null && fragment.isAdded()) { ensureTransaction(); fragmentTransaction.remove(fragment); } }
@Override protected boolean isComponentValid(android.support.v4.app.Fragment fragment) { return fragment.isAdded(); }
public void setFragment(String fragmentName) { Fragment temp = fragmentManager.findFragmentByTag(fragmentName); if (temp != null && temp.isAdded()) { // L.t(getApplicationContext(),fragmentName + " is Added already"); if (currentFrag != null && !currentFrag.getTag().contains(fragmentName)) { // L.t(getApplicationContext(), currentFrag.getTag() +" was current"); fragmentManager .beginTransaction() .hide(currentFrag) .show(temp) // .addToBackStack(fragmentName) .commit(); } // When rotation a fragment is added, however current fragment is null. currentFrag = temp; return; } else if (temp != null) { // when using retainInstance(true) the lifecycle may change. // L.t(getApplicationContext(), fragmentName + " not null but is not added"); } try { Class cl = Class.forName(fragmentName); Constructor con = cl.getConstructor(); temp = (Fragment) con.newInstance(); // First Fragment created if (currentFrag == null) { fragmentManager .beginTransaction() .add(R.id.mainframe, temp, fragmentName) // .addToBackStack(fragmentName) .commit(); } else { fragmentManager .beginTransaction() .hide(currentFrag) .add(R.id.mainframe, temp, fragmentName) // .addToBackStack(fragmentName) .commit(); } currentFrag = temp; return; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassCastException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), "Reflection error", Toast.LENGTH_SHORT).show(); }
@Override public void run() { if (fragment.isAdded()) { showContent(); } }