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

The following examples show how to use android.support.v4.app.FragmentTransaction#commitNowAllowingStateLoss() . 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: FragmentUtility.java    From scene with Apache License 2.0 5 votes vote down vote up
static void commitFragment(@NonNull FragmentTransaction transaction, boolean commitNow) {
    if (commitNow) {
        transaction.commitNowAllowingStateLoss();
    } else {
        transaction.commitAllowingStateLoss();
    }
}
 
Example 2
Source File: BaseMainFragment.java    From v9porn with MIT License 5 votes vote down vote up
/**
 * 尝试释放内存,因为我们使用的是FragmentPagerAdapter,导致即使页面不可见也不会释放内存,好处在于返回该页面时所有状态视图会保持为之前的状态
 */
@Subscribe(threadMode = ThreadMode.MAIN)
public void onTryToReleaseMemory(LowMemoryEvent lowMemoryEvent) {
    //有可能已经被回收或者还没创建(概率)
    if (!isBackground || categoryList == null || fragmentManager == null || viewPager == null || mBaseMainFragmentAdapter == null) {
        return;
    }
    if (!BuildConfig.DEBUG) {
        //Bugsnag.notify(new Throwable(TAG + ":LowMemory,try to release some memory now!"), Severity.INFO);
    }
    try {
        Logger.t(TAG).d("start try to release memory ....");
        FragmentTransaction mCurTransaction = fragmentManager.beginTransaction();

        //获取当前不可见的fragment,并尝试释放掉它们来释放内存,但同时会导致切换回该页面时所有状态均没有了,会被重新创建
        for (int i = 0; i < categoryList.size(); i++) {
            //尝试移除不可见的fragment
            if (i < currentSelectPosition - 1 || i > currentSelectPosition + 1) {
                long itemId = mBaseMainFragmentAdapter.getItemId(i);
                String name = FragmentUtils.makeFragmentName(viewPager.getId(), itemId);
                Fragment fragment = fragmentManager.findFragmentByTag(name);
                if (fragment != null) {
                    mCurTransaction.remove(fragment);
                }
            }
        }
        mCurTransaction.commitNowAllowingStateLoss();
        //通知系统尝试释放内存
        System.gc();
        System.runFinalization();
        Logger.t(TAG).d("try to release memory success !!!");
    } catch (Exception e) {
        e.printStackTrace();
        if (!BuildConfig.DEBUG) {
            //Bugsnag.notify(new Throwable(TAG + " tryToReleaseMemory error::", e), Severity.WARNING);
        }
    }
}
 
Example 3
Source File: BaseMainFragment.java    From v9porn with MIT License 5 votes vote down vote up
/**
 * 尝试释放内存,因为我们使用的是FragmentPagerAdapter,导致即使页面不可见也不会释放内存,好处在于返回该页面时所有状态视图会保持为之前的状态
 */
@Subscribe(threadMode = ThreadMode.MAIN)
public void onTryToReleaseMemory(LowMemoryEvent lowMemoryEvent) {
    //有可能已经被回收或者还没创建(概率)
    if (!isBackground || categoryList == null || fragmentManager == null || viewPager == null || mBaseMainFragmentAdapter == null) {
        return;
    }
    if (!BuildConfig.DEBUG) {
        //Bugsnag.notify(new Throwable(TAG + ":LowMemory,try to release some memory now!"), Severity.INFO);
    }
    try {
        Logger.t(TAG).d("start try to release memory ....");
        FragmentTransaction mCurTransaction = fragmentManager.beginTransaction();

        //获取当前不可见的fragment,并尝试释放掉它们来释放内存,但同时会导致切换回该页面时所有状态均没有了,会被重新创建
        for (int i = 0; i < categoryList.size(); i++) {
            //尝试移除不可见的fragment
            if (i < currentSelectPosition - 1 || i > currentSelectPosition + 1) {
                long itemId = mBaseMainFragmentAdapter.getItemId(i);
                String name = FragmentUtils.makeFragmentName(viewPager.getId(), itemId);
                Fragment fragment = fragmentManager.findFragmentByTag(name);
                if (fragment != null) {
                    mCurTransaction.remove(fragment);
                }
            }
        }
        mCurTransaction.commitNowAllowingStateLoss();
        //通知系统尝试释放内存
        System.gc();
        System.runFinalization();
        Logger.t(TAG).d("try to release memory success !!!");
    } catch (Exception e) {
        e.printStackTrace();
        if (!BuildConfig.DEBUG) {
            //Bugsnag.notify(new Throwable(TAG + " tryToReleaseMemory error::", e), Severity.WARNING);
        }
    }
}
 
Example 4
Source File: FragmentPageSnapAdapter.java    From RecyclerPager with Apache License 2.0 5 votes vote down vote up
/**
 * Attach Fragment,
 */
@Override public void onViewAttachedToWindow(@NonNull FragmentViewHolder holder) {
    String name = makeFragmentName(holder.itemView.getId(), holder.getAdapterPosition());
    Fragment fragment = mFragmentManager.findFragmentByTag(name);

    if (fragment != null && fragment == holder.currentFragment) {
        // Nothing Changed
    } else {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

        if (fragment != null) {
            fragmentTransaction.attach(fragment);
        } else {
            fragment = getItem(holder.getAdapterPosition());
            fragmentTransaction.add(holder.itemView.getId(), fragment, name);
        }
        fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);

        fragmentTransaction.commitNowAllowingStateLoss();

        if (holder.currentFragment != null && holder.currentFragment.getUserVisibleHint()) {
            holder.currentFragment.setMenuVisibility(false);
            holder.currentFragment.setUserVisibleHint(false);
        }

        holder.currentFragment = fragment;
    }
}
 
Example 5
Source File: FragmentRecyclerAdapter.java    From RecyclerPager with Apache License 2.0 5 votes vote down vote up
/**
 * Attach Fragment
 */
@Override public void onViewAttachedToWindow(@NonNull FragmentViewHolder holder) {

    String name = makeFragmentName(holder.itemView.getId(), holder.getAdapterPosition());
    Fragment fragment = mFragmentManager.findFragmentByTag(name);

    if (fragment != null && fragment == holder.currentFragment) {
        // Nothing Changed
    } else {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

        if (fragment != null) {
            fragmentTransaction.attach(fragment);
        } else {
            fragment = getItem(holder.getAdapterPosition());
            fragmentTransaction.add(holder.itemView.getId(), fragment, name);
        }
        fragmentTransaction.commitNowAllowingStateLoss();

        if (holder.currentFragment != null && holder.currentFragment.getUserVisibleHint()) {
            holder.currentFragment.setMenuVisibility(false);
            holder.currentFragment.setUserVisibleHint(false);
        }

        holder.currentFragment = fragment;
        if (!fragment.getUserVisibleHint()) {
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
        }
    }
}
 
Example 6
Source File: FragmentRecyclerAdapter.java    From RecyclerPager with Apache License 2.0 5 votes vote down vote up
/**
 * Detach Fragment
 */
@Override public void onViewDetachedFromWindow(@NonNull FragmentViewHolder holder) {
    if (holder.currentFragment != null) {
        if (holder.currentFragment.getUserVisibleHint()) {
            holder.currentFragment.setMenuVisibility(false);
            holder.currentFragment.setUserVisibleHint(false);
        }
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.detach(holder.currentFragment);
        fragmentTransaction.commitNowAllowingStateLoss();
    }
    holder.currentFragment = null;
}
 
Example 7
Source File: FragmentRecyclerAdapter.java    From RecyclerPager with Apache License 2.0 5 votes vote down vote up
/**
 * Detach Fragment
 */
@Override public void onViewRecycled(@NonNull FragmentViewHolder holder) {
    if (holder.currentFragment != null) {
        if (holder.currentFragment.getUserVisibleHint()) {
            holder.currentFragment.setMenuVisibility(false);
            holder.currentFragment.setUserVisibleHint(false);
        }
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        fragmentTransaction.detach(holder.currentFragment);
        fragmentTransaction.commitNowAllowingStateLoss();
    }
    holder.currentFragment = null;
}
 
Example 8
Source File: FragmentStateManager.java    From FragmentStateManager with MIT License 5 votes vote down vote up
/**
 * Shows fragment at position and detaches previous fragment if exists. If fragment is found in
 * fragment manager, it is reattached else added.
 *
 * @param position
 * @return fragment at position
 */
public Fragment changeFragment(int position) {
    String tag = makeFragmentName(container.getId(), getItemId(position));
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

    /*
      If fragment manager doesn't have an instance of the fragment, get an instance
      and add it to the transaction. Else, attach the instance to transaction.
     */
    Fragment fragment = mFragmentManager.findFragmentByTag(tag);
    if (fragment == null) {
        fragment = getItem(position);
        fragmentTransaction.add(container.getId(), fragment, tag);
    } else {
        fragmentTransaction.attach(fragment);
    }

    // Detach existing primary fragment
    Fragment curFrag = mFragmentManager.getPrimaryNavigationFragment();
    if (curFrag != null) {
        fragmentTransaction.detach(curFrag);
    }

    // Set fragment as primary navigator for child manager back stack to be handled by system
    fragmentTransaction.setPrimaryNavigationFragment(fragment);
    fragmentTransaction.setReorderingAllowed(true);
    fragmentTransaction.commitNowAllowingStateLoss();

    return fragment;
}
 
Example 9
Source File: FragmentStateManager.java    From FragmentStateManager with MIT License 5 votes vote down vote up
/**
 * Removes Fragment from Fragment Manager and clears all saved states. Call to changeFragment()
 * will restart fragment from fresh state.
 *
 * @param position
 */
public void removeFragment(int position) {
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.remove(mFragmentManager
            .findFragmentByTag(makeFragmentName(container.getId(), getItemId(position))));
    fragmentTransaction.commitNowAllowingStateLoss();
}
 
Example 10
Source File: FragmentUtil.java    From px-android with MIT License 5 votes vote down vote up
public static void tryRemoveNow(@NonNull final FragmentManager fragmentManager, @NonNull final String tag) {
    final Fragment fragment = fragmentManager.findFragmentByTag(tag);
    if (fragment != null) {
        final FragmentTransaction transaction = fragmentManager.beginTransaction().remove(fragment);
        try {
            transaction.commitNowAllowingStateLoss();
        } catch (final IllegalStateException e) {
            transaction.commitAllowingStateLoss();
        }
    }
}