Alternative for the onResume() during Fragment switching

后端 未结 6 1589
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 13:25

onResume() method won\'t get called when we switch between fragments more than one time. So, is there any better way to handle resume operation?

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 14:09

    You can try this,

    Step1: Override the Tabselected method in your activity

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        try {
        if(MyEventsFragment!=null && tab.getPosition()==3)
        {
            MyEvents.fragmentChanged();
        }
        }
        catch (Exception e)
        {
    
        }
        mViewPager.setCurrentItem(tab.getPosition());
    }
    

    Step 2: Using static method do what you want in your fragment,

    public static void fragmentChanged()
    {
        Toast.makeText(actvity, "Fragment Changed", Toast.LENGTH_SHORT).show();
    }
    

提交回复
热议问题