简体   繁体   English

在Android的SherlockFragmentActivity的SherlockFragment中未调用onResume()吗?

[英]onResume() is not called in SherlockFragment of SherlockFragmentActivity in Android?

I am working in an application in which I have used the Sherlock Fragment API. 我正在使用Sherlock Fragment API的应用程序中工作。

I am having 6 SherlockFragments for making 6 different screens. 我有6个SherlockFragments ,可以制作6个不同的屏幕。 Now when my application is launched, initially the onResume for all the fragments are called at once. 现在,启动我的应用程序时,最初将立即调用所有片段的onResume。

I am in a need of a method like in activity we use onResume for moving from one Fragment to another Fragment. 我需要一种像在活动中那样的方法,我们使用onResume从一个片段移动到另一个片段。 I have to call a web service method when I move from one fragment to another fragment. 从一个片段移到另一个片段时,我必须调用Web服务方法。

I tried to use the onResume here, but when I move from 1st to 2nd fragment, then the onResume of the 3rd fragment is called. 我尝试在此处使用onResume ,但是当我从第一个片段移动到第二个片段时,将调用第三个片段的onResume。 Alternate fragment is called when we move from one fragment to then next. 当我们从一个片段移动到另一个片段时,将调用备用片段。

Please suggest me, which method should I use to call my web service when I move from 1 fragment to next fragment. 请建议我,当我从1个片段移动到下一个片段时,应该使用哪种方法来调用Web服务。

EDITED : My actual requirement is to have an event which is called when we a Fragment is visible to user. 编辑 :我的实际要求是要有一个事件,当我们对用户可见“片段”时将调用该事件。 At that event I have to call service for getting updated data. 在那种情况下,我必须致电服务中心以获取更新的数据。

The onResume method is tied to the Activity . onResume方法与Activity绑定。 If you're moving back and forth between Fragments attached to a common Activity , onResume won't be called again. 如果您在连接到常见Activity Fragments之间来回移动,则不会再次调用onResume However, if you were to press back or navigate away from this Activity and then come back, onResume would be called. 但是,如果您要按回去或离开该Activity然后再返回,则会调用onResume

You might consider onAttach . 您可以考虑onAttach

You can use this way for fix this issue. 您可以使用这种方法来解决此问题。 Actually I was also facing same problem in my application. 实际上,我的应用程序中也遇到了同样的问题。 I was using FragmentPagerAdapter for show multiple tab in ViewPager. 我正在使用FragmentPagerAdapter在ViewPager中显示多个选项卡。 I have just hide and show fragment on TabSelection with notifyDataChanged() method for refresh adapter. 我只是使用刷新适配器的notifyDataChanged()方法在TabSelection上隐藏并显示了片段。

My code: 我的代码:

/** Creating an instance of FragmentPagerAdapter */
final MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);

/** Setting the FragmentPagerAdapter object to the viewPager object */
_mPager.setAdapter(fragmentPagerAdapter);
_mActionBar.setDisplayShowTitleEnabled(false);

/** Defining tab listener */
ActionBar.TabListener tabListener = new ActionBar.TabListener() {

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {

            ft.hide(fragmentPagerAdapter.getItem(tab.getPosition()));
            fragmentPagerAdapter.notifyDataSetChanged();
            _mPager.setAdapter(fragmentPagerAdapter);
        }

        @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
            _mPager.setCurrentItem(tab.getPosition());

        ft.show(fragmentPagerAdapter.getItem(tab.getPosition()));
            fragmentPagerAdapter.notifyDataSetChanged();
            _mPager.setAdapter(fragmentPagerAdapter);
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
};

This is worked for me. 这对我有用。 I've used onStart() rather than using onresume() in sherlockfragmentactivity . 我在sherlockfragmentactivity使用了onStart()而不是onresume() And I called to the notifyondatasetchanged . 然后我打电话给notifyondatasetchanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM