不了解viewpager生命周期与片段

时间:2013-10-06 15:27:50

标签: android android-fragments actionbarsherlock android-viewpager android-fragmentactivity

我对有点碎片的viewpager有点失望,因为4天我尝试了许多解决方案,没有成功。

我是碎片的初学者,我按照一个很棒的教程来创建我的viewpager。我可以用手机旋转手机没问题。但我必须从viewpager中的片段打开一个新活动,然后返回包含viewpager的主机活动。在这一点上,当我回到活动B时,我无法从主机活动中获取我的片段。我不明白我能做什么?

例如:

TabSessionActivity - >片段A - >活动B - > TabsessionActivity(片段A = NULL)

TabSessionActivity - >片段A - >旋转手机 - > TabsessionActivity(片段A OK!)

这是我的适配器:

public class MyPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> fragments;
    private FragmentManager mFragmentManager; 

    // On fournit à l'adapter la liste des fragments à afficher
    public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        mFragmentManager=fm;

        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        for(Fragment mfragment:fragments){
            Log.i("Fragment Tag and Id"," FragmentTag:"+mfragment.getTag()+" FragmentID:"+mfragment.getId()+"NB fragments:"+this.fragments.size());

        }
        return this.fragments.get(position);

    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }





    public Fragment getActiveFragment(ViewPager container, int position) {
        String name = makeFragmentName(container.getId(), position);
        for(Fragment mfragment:mFragmentManager.getFragments()){
            Log.i("Fragment Tag and Id"," FragmentTag:"+mfragment.getTag()+" FragmentID:"+mfragment.getId()+" NB fragments:"+this.mFragmentManager.getFragments().size());
        }
        return  mFragmentManager.findFragmentByTag(name);

        }

        private static String makeFragmentName(int viewId, int index) {
            return "android:switcher:" + viewId + ":" + index;
        }
}

然后我的主持人活动:

public class TabsSessionActivity extends SherlockFragmentActivity {
    private GeoLocatingService mGeoService;
    private MyPagerAdapter mPagerAdapter;  
    private  ViewPager mViewPager;
//  private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
    private Dashboard_Fragment fragment1;
    private OsmMapFragment fragment2;
    private Fragment1 fragment3;

    final IGeoLocatingServiceListener servicelistener = new IGeoLocatingServiceListener() {
        @Override
        public void dataChanged(final Object data) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.i("TabSessionActivity:GeoService","Service rafraichi");
                    refreshCurrentFragment();

                }
            });
        }
    };

    /************************** SERVICE CONNECTION **********************/
    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("GeoService", "TabsSessionActivity:Disconnected!");
            mGeoService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            Log.i("TabSessionActivity:GeoService", "TabsSessionActivity:Connected!");
            mGeoService = ((GeoLocatingService.LocalBinder) service).getService();
            mGeoService.addListener(servicelistener);
            Log.i("TabSessionActivity:GeoService","Service connecté");
            refreshCurrentFragment();





        }
    };

    public GeoLocatingService getGeoLocatingService() {
        return mGeoService;
    }




    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the view from main_fragment.xml
        setContentView(R.layout.activity_tabs_session);

        // Création de la liste de Fragments que fera défiler le PagerAdapter
        List fragments = new Vector();

        // Ajout des Fragments dans la liste
        fragments.add(Fragment.instantiate(this,OsmMapFragment.class.getName()));
        fragments.add(Fragment.instantiate(this,Dashboard_Fragment.class.getName()));
        fragments.add(Fragment.instantiate(this,Fragment1.class.getName()));

        // Création de l'adapter qui s'occupera de l'affichage de la liste de
        // Fragments
        this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(),fragments);



        // Affectation de l'adapter au ViewPager
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(this.mPagerAdapter);

      //Bind the title indicator to the adapter
        LinePageIndicator LineIndicator = (LinePageIndicator)findViewById(R.id.indicator);
        LineIndicator.setViewPager(mViewPager);


        //Style Line Indicator
        final float density = getResources().getDisplayMetrics().density;
        LineIndicator.setSelectedColor(0xFF45A5D7);
        LineIndicator.setUnselectedColor(0xFF888888);
        LineIndicator.setStrokeWidth(4 * density);
        LineIndicator.setLineWidth(30 * density);

        mViewPager.setCurrentItem(1);
        Log.i("TabSessionActivity:GeoService","On Create");


    }

    public void refreshCurrentFragment(){
        //Retrieve fragments instances


        fragment1 = (Dashboard_Fragment) this.mPagerAdapter.getActiveFragment(mViewPager, 1);
        fragment2 = (OsmMapFragment) this.mPagerAdapter.getActiveFragment(mViewPager, 0);
        fragment3 = (Fragment1)this.mPagerAdapter.getActiveFragment(mViewPager, 2);

        //Check if fragment is active and visible
        if (fragment1!=null){
            Log.i("TabSessionActivity:GeoService","Fragment trouvé");
            fragment1.onDatachangedCallback();
        }
        if (fragment2!=null){
            fragment2.onDatachangedCallback();
        }
        if (fragment3!=null){
            fragment3.onDatachangedCallback();
        }

    }



    @Override
    public void onStart() {
        super.onStart();
        //On attache la vue au service
        bindService(
                new Intent(this, GeoLocatingService.class),
                connection, BIND_AUTO_CREATE);
        Log.i("TabSessionActivity:GeoService","On start bind");
    }



    @Override
    public void onResume() {
        super.onResume();
            if(mGeoService!=null){
                Log.i("TabSessionActivity:GeoService","RefreshCurrentFragment()");
            }

    }

    @Override
    public void onPause() {
        super.onPause();
            if(mGeoService!=null){
                unbindService(connection);
                mGeoService.removeListener(servicelistener);
                mGeoService=null;
                Log.i("TabSessionActivity:GeoService","On pause unbind");
            }


    }

    @Override
    public void onDestroy() {
         if(mGeoService!=null){
                unbindService(connection);
                Log.i("TabSessionActivity:GeoService","On destroy unbind");
            }
        /*if(ServiceTools.isGeoLocatingServiceRunning(this)){
            stopService(new Intent(this, GeoLocatingService.class));
        }*/

        super.onDestroy();
    }



    @Override
    public boolean onKeyDown(int keycode, KeyEvent event) {
        if (keycode == KeyEvent.KEYCODE_BACK) {
            if(mGeoService!=null){
                if(mGeoService.getRecord()){
                showCancelRecordAlertToUser();
                }else{
                    startActivity(new Intent(this, MainActivity.class));
                    finish();
                }

            }else{
                startActivity(new Intent(this, MainActivity.class));
                finish();
            }

        }
        return super.onKeyDown(keycode, event);
    }

当我从活动B返回主机活动时,Log Cat对片段说null:

fragment1 = (Dashboard_Fragment) this.mPagerAdapter.getActiveFragment(mViewPager, 1);
            fragment2 = (OsmMapFragment) this.mPagerAdapter.getActiveFragment(mViewPager, 0);
            fragment3 = (Fragment1)this.mPagerAdapter.getActiveFragment(mViewPager, 2);

1 个答案:

答案 0 :(得分:-1)

试试这个:

onUserInteraction(){
    super.onUserInteraction();
    switch (mViewPager.getCurrentItem()) {
    case 3:
        Fragment currentFragment = this.getSupportFragmentManager()
                `.findFragmentByTag(getFragmentTag(mViewPager.getCurrentItem()));
        break;
   }

private String getFragmentTag(int pos) {
    return "android:switcher:" + R.id.viewpager + ":" + pos;
}

记录currentFragment值并检查它是否为NULL。这对我有用。