从ActionBar选项卡获取当前片段

时间:2013-01-16 04:50:10

标签: android android-fragments android-actionbar

我有两个片段(Fragment1和Fragment2),使用自定义TabListener类创建ActionBar导航。每个标签的内容都是我的一个片段。但现在我遇到了问题 - 我无法访问/获取片段的实例。

public void onCreate( Bundle savedInstanceState )
    {   
        super.onCreate( savedInstanceState );
        getSupportActionBar().setNavigationMode( getSupportActionBar().NAVIGATION_MODE_TABS );

        Tab tab = getSupportActionBar().newTab()
            .setText( R.string.frag1 )
            .setTabListener( 
                new MyTabListener( this, 
                    Fragment1.class.getName() ) );
        getSupportActionBar().addTab( tab );

        tab = getSupportActionBar().newTab()
            .setText( R.string.frag2 )
            .setTabListener( 
                new MyTabListener( this, 
                    Fragment2.class.getName() ) );
        getSupportActionBar().addTab( tab );

        Fragment1 f1 = (Fragment1) getSupportFragmentManager().findFragmentByTag("FRAG1");
        if(f1 != null) {
            Toast.makeText(this, "It's not empty", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(this, "Photo Intent Goes Here", Toast.LENGTH_SHORT).show();

        if(data != null) {
            Toast.makeText(this, "It's not empty", Toast.LENGTH_SHORT).show();

             if (resultCode == RESULT_OK) {  


                }  

        } else {
            Toast.makeText(this, "It's empty", Toast.LENGTH_SHORT).show();
        }
    }

    private class MyTabListener  implements TabListener
    {
        private SherlockFragment mFragment;
        private final Activity mActivity;
        private final String mFragName;

        public MyTabListener( Activity activity, 
            String fragName )
        {
            mActivity = activity;
            mFragName = fragName;
        }


        @Override
        public void onTabSelected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            mFragment = (SherlockFragment) SherlockFragment.instantiate( mActivity, 
                    mFragName );

            if( mFragment instanceof Fragment1 ) {
                Fragment1 f1temp = (Fragment1) mFragment;

                //Toast.makeText(mActivity, "Frag1", Toast.LENGTH_SHORT).show();

                  ft.add( android.R.id.content, mFragment,"FRAG1");
            }

            else {
                  ft.add( android.R.id.content, mFragment,"ELSE");
            }


            //Toast.makeText(mActivity, mFragName, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onTabUnselected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            ft.remove( mFragment );
            mFragment = null;

        }

        public SherlockFragment getCurrentFragment() {
            return mFragment;
        }

        @Override
        public void onTabReselected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

    }

我无法使用getSupportFragmentManager().findFragmentById( id ),因为我没有身份证明我也尝试了getSupportFragmentManager().findFragmentByTag( tag ),但它也无效,因为我只调用了add()方法FragmentTransaction class not replace()

是否有办法获取当前的标签片段实例,以便我可以调用片段的方法?

2 个答案:

答案 0 :(得分:5)

我找到了解决方案。我试图在onActivityResult()方法中获取片段的实例,并且实际上有效。

 public void onActivityResult(int requestCode, int resultCode, Intent data) {

Toast.makeText(this, "Photo Intent Goes Here", Toast.LENGTH_SHORT).show();

        if(data != null) {
            Toast.makeText(this, "It's not empty", Toast.LENGTH_SHORT).show();

             if (resultCode == RESULT_OK) {  
                   Fragment1 f1 = (Fragment1) getSupportFragmentManager().findFragmentByTag("FRAG1");
                  f1.onCameraResult(requestCode,resultCode,data); // Your method   of the fragment

                }  

        } else {
            Toast.makeText(this, "It's empty", Toast.LENGTH_SHORT).show();
        }
    }

答案 1 :(得分:0)

我从FragmentPagerAdapter实例获取片段:

FragmentPagerAdapter adapter = (FragmentPagerAdapter) viewPager.getAdapter();
Fragment fragment = adapter.getItem(tabLayout.getSelectedTabPosition());