我在Android中玩碎片。

我知道我可以通过使用以下代码更改一个片段:

FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction fragTrans = fragMgr.beginTransaction();

MyFragment myFragment = new MyFragment(); //my custom fragment

fragTrans.replace(android.R.id.content, myFragment);
fragTrans.addToBackStack(null);
fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragTrans.commit();

我的问题是,在Java文件中,如何获得当前显示的片段实例?


当前回答

我认为你们想知道u在哪个片段中 我想有一个解决办法,但我认为它不是最好的 但这是可行的 首先


你应该创建你的父片段,它扩展fragment


public class Fragment2 extends Fragment {
    private static position;
    public static int getPosition() {
        return position;
    }

    public static void setPosition(int position) {
        FragmentSecondParent.position = position;
    }
}

第二个

你应该在每个片段和每次写入onCreateView中扩展它

setPosition(//your fragmentposition here)

第三


你想在哪里得到当前的片段,你应该


写这篇文章

Fragment2 fragment= (Fragment2) getSupportFragmentManager()
    .findFragmentById(R.id.fragment_status);

int position = fragment.getPosition();
if(//position condition)
{
    //your code here
}

其他回答

In the main activity, the onAttachFragment(Fragment fragment) method is called when a new fragment is attached to the activity. In this method, you can get the instance of the current fragment. However, the onAttachFragment(Fragment fragment) method is not called when a fragment is popped off the back stack, ie, when the back button is pressed to get the top fragment on top of the stack. I am still looking for a callback method that is triggered in the main activity when a fragment becomes visible inside the activity.

在这些情况下,使用事件总线(如Otto、EventBus或RxJava总线)特别方便。

虽然这种方法不一定会将当前可见的片段作为对象传递给您(尽管也可以这样做,但这会导致更长的调用链),但它允许您在当前可见的片段上执行操作(这通常是您想要知道当前可见的片段所做的事情)。

确保您尊重片段生命周期,并在适当的时间注册/取消注册事件总线 此时,您需要知道当前可见的片段并执行一组操作。使用事件总线输出事件。

所有已注册到总线的可见片段都将执行必要的操作。

我也坚持这一点。我最后做的,只是声明了一个Fragments数组:

private static PlaceholderFragment[] arrFrg;

(在我的情况下,它是PlaceholderFragment)和包装所有这些片段到这个数组没有标记:)

        public static PlaceholderFragment newInstance(int sectionNumber) {
            final PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            arrFrg[sectionNumber] = fragment;

            return fragment;
}

然后你可以很容易地访问当前显示的片段:

arrFrg[mViewPager.getCurrentItem()];

我知道,这可能不是最好的解决方案,但它对我来说非常适合:)

签出此解决方案。我成功拿到了现在的碎片。

if(getSupportFragmentManager().getBackStackEntryCount() > 0){
        android.support.v4.app.Fragment f = 
         getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        if(f instanceof ProfileFragment){
            Log.d(TAG, "Profile Fragment");
        }else if(f instanceof SavedLocationsFragment){
            Log.d(TAG, "SavedLocations Fragment");
        }else if(f instanceof AddLocationFragment){
            Log.d(TAG, "Add Locations Fragment");
        }

请尝试这种方法.....

private Fragment getCurrentFragment(){
    FragmentManager fragmentManager = getSupportFragmentManager();
    String fragmentTag = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1).getName();
    Fragment currentFragment = getSupportFragmentManager()
.findFragmentByTag(fragmentTag);
    return currentFragment;
}