片段切换过程中onResume()的替代方法

当我们多次切换片段时, onResume()方法不会被调用。 那么,有没有更好的办法来处理恢复操作?

  • 片段onResume不叫
  • Android onResume更新列表适配器
  • Android手机的锁屏会导致多个后续的onPause / onResume事件
  • 无法恢复活动
  • 在应用程序/任务级别获取onPause&onResume像事件一样
  • 我如何重新启动在Android的活动?
  • Android:“应用程序级别”暂停和恢复
  • 重写Android的所有活动的OnResume()方法
  • 代码如下:

    步骤1

    创build界面

     public interface YourFragmentInterface { void fragmentBecameVisible(); } 

    第2步

    附加列表器在setOnPageChangeListener中

     mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(final int position, final float v, final int i2) { } @Override public void onPageSelected(final int position) { YourFragmentInterface fragment = (YourFragmentInterface) mPagerAdapter.instantiateItem(mViewPager, position); if (fragment != null) { fragment.fragmentBecameVisible(); } } @Override public void onPageScrollStateChanged(final int position) { } }); 

    步骤:3

    在你的片段中实现接口

     public class yourActivity extends SherlockFragment implements YourFragmentInterface{ @Override public void fragmentBecameVisible() { System.out.println("TestFragment"); } } 

    在片段切换期间,在所有的片段活动fragmentBecameVisible()实现此列表器。

    谢谢。

    我想,我find了答案。 这里是每当片段变得可见时将强制片段的onResume()被调用的链接。
    片段onResume从后面堆栈

    您可以通过在select页面时在每个fragment上提供public方法来恢复工作,我举例说明:

     public class MainActivity extends FragmentActivity { private TabHost mTabHost; private ViewPager mViewPager; private TabsAdapter mTabsAdapter; private ArrayList<Fragment> mFragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTabHost = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup(); mViewPager = (ViewPager) findViewById(R.id.pager); mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager); mFragments = new ArrayList<Fragment>(); mFragments.add(new TestFragment1()); mFragments.add(new TestFragment2()); mFragments.add(new TestFragment3()); mTabsAdapter.addTab(mTabHost.newTabSpec("fragment1").setIndicator("fragment1")); mTabsAdapter.addTab(mTabHost.newTabSpec("fragment2").setIndicator("fragment2")); mTabsAdapter.addTab(mTabHost.newTabSpec("fragment3").setIndicator("fragment3")); if (savedInstanceState != null) { mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("tab", mTabHost.getCurrentTabTag()); } private void refreshPage(int i) { Fragment fragment = mFragments.get(i); switch (i) { case 0: ((TestFragment1) fragment).refreshView(); break; case 1: ((TestFragment2) fragment).refreshView(); break; case 2: ((TestFragment3) fragment).refreshView(); break; } } class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener { private final Context mContext; private final TabHost mTabHost; private final ViewPager mViewPager; public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) { super(activity.getSupportFragmentManager()); mContext = activity; mTabHost = tabHost; mViewPager = pager; mTabHost.setOnTabChangedListener(this); mViewPager.setAdapter(this); mViewPager.setOnPageChangeListener(this); } public void addTab(TabHost.TabSpec tabSpec) { tabSpec.setContent(new DummyTabFactory(mContext)); mTabHost.addTab(tabSpec); notifyDataSetChanged(); } @Override public Fragment getItem(int i) { return mFragments.get(i); } @Override public void onPageScrolled(int i, float v, int i2) { } @Override public void onPageSelected(int i) { TabWidget widget = mTabHost.getTabWidget(); int oldFocusability = widget.getDescendantFocusability(); widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); mTabHost.setCurrentTab(i); widget.setDescendantFocusability(oldFocusability); refreshPage(i); } @Override public void onPageScrollStateChanged(int i) { } @Override public void onTabChanged(String s) { int postion = mTabHost.getCurrentTab(); mViewPager.setCurrentItem(postion, true); } @Override public int getCount() { return mFragments.size(); } class DummyTabFactory implements TabHost.TabContentFactory { private final Context mContext; public DummyTabFactory(Context context) { mContext = context; } @Override public View createTabContent(String s) { View v = new View(mContext); v.setMinimumWidth(0); v.setMinimumHeight(0); return v; } } } } 

    然后你写下你的Fragment

     public class TestFragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.main, container, false); } public void refreshView() { //do whatever you want } } 

    你可以试试这个,

    第1步:覆盖您活动中的Tabselected方法

     @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()); } 

    第2步:使用静态方法做你想要在你的片段,

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

    我已经使用片段覆盖方法解决了这个问题:

      @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { if (getView() != null) { // your code goes here } } } 

    注意:在ViewPager的第一个片段setUserVisibleHint之前调用onCreateView,所以你需要处理这个。

    在片段中的onCreateView之前调用setUserVisibleHint

    你可以简单地通过使用一个接口为每个片段模拟onResume()!

    1-创build一个名为FragmentLifecycle的接口,代码如下:

     public interface FragmentLifecycle { public void onResumeFragment(); } 

    2-让每个片段实现接口:

     public class FragmentOne extends Fragment implements FragmentLifecycle 

    3-在每个片段中实现接口方法:

     @Override public void onResumeFragment() { Log.i(TAG, "onResumeFragment()"); Toast.makeText(getActivity(), "onResumeFragment():" + TAG,Toast.LENGTH_SHORT).show(); } 

    4-在ViewPager页面上调用接口方法页面变化:

     viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { FragmentLifecycle fragmentToShow = (FragmentLifecycle)adapter.getItem(position); fragmentToShow.onResumeFragment(); } }); 

    请享用!