我在下面的片段中启动处理程序:
public class FeedsFragment extends Fragment{
private Handler handler;
private Runnable runnableCode;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create the Handler object (on the main thread by default)
handler = new Handler();
// Define the task to be run here
runnableCode = new Runnable() {
@Override
public void run() {
// Get the No of New Feeds
getFeedCounts();
// Repeat this runnable code again every 30 seconds
handler.postDelayed(runnableCode, 30,000);
}
};
handler.post(runnableCode);
}
@Override
public void onDetach() {
super.onDetach();
handler.removeCallbacksAndMessages(runnableCode);
}
@Override
public void onDestroyView() {
handler.removeCallbacksAndMessages(runnableCode);
super.onDestroyView();
}
@Override
public void onPause() {
super.onPause();
handler.removeCallbacksAndMessages(runnableCode);
}
@Override
public void onResume() {
/**
* Start the handler again if coming back from any profile page
* or hashtag search
*/
isHandlerStopped = false;
handler.post(runnableCode);
super.onResume();
}
我正在删除onDetach和onDestroyView函数中的所有回调,但它无法正常工作。
当我在同一个活动中加载一个不同的片段时,这个处理程序 不会停止自己,只有当我加载其他一些时,处理程序才会停止 我的申请中的活动。
我希望这个处理程序只在我在其中使用它的片段被加载到活动中时运行,如果在同一个活动中加载了一些其他片段,它应该被停止。
此外,此片段位于viewpager内部,因此当我切换选项卡并且其他片段被加载时,处理程序不会停止。请在活动中查看以下代码:
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
FeedsFragment feedsFragment;
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// when user do a swipe the selected tab change
switch (position){
case 0:
break;
case 1:
feedsFragment.removeHandler();
break;
case 2:
feedsFragment.removeHandler();
break;
case 3:
feedsFragment.removeHandler();
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
FragmentAdapter adapter = new FragmentAdapter(getActivity().getSupportFragmentManager());
feedsFragment = FeedsFragment.newInstance(bundle);
adapter.addFragmentWithIcon(FeedsFragment.newInstance(null))
, mContext.getResources().getString(R.string.feed_tab_name));
adapter.addFragmentWithIcon(FeedFriendRequests.newInstance(null),
mContext.getResources().getString(R.string.requests_tab_name));
adapter.addFragmentWithIcon(NewMessagesFragment.newInstance(null), mContext.getResources().getString(R.string.message_tab_name));
adapter.addFragmentWithIcon(NotificationFragment.newInstance(null)
, mContext.getResources().getString(R.string.notification_tab_name));
pager.setAdapter(adapter);
当我在那时选择任何其他标签时,我在FeedFragment中调用方法removeHandler。
public void removeHandler(){
handler.removeCallbacks(runnableCode);
}
但是处理程序仍然被调用。
如果有人对此有所了解,请帮帮我。
提前多多感谢
答案 0 :(得分:3)
我不认为它与您的片段生命周期有关。你需要替换
handler.removeCallbacksAndMessages(runnableCode);
// this does not seem to work
带
handler.removeCallbacks(runnableCode);
另一种方法是在FeedsFragment中创建一个方法
public void removeHandler()
{
Log.i("Stop Handler ","Yes");
handler.removeCallbacks(runnableCode);
}
在您想要替换片段
时的活动中if(feedsFragment.isAdded())
{
feedsFragment.removeHandler();
}
...// rest of the code to replace the fragment with new one
编辑:
viewPager.addOnPageChangeListener(pageChangeListener);
viewPager.post(new Runnable() {
@Override
public void run() {
pageChangeListener.onPageSelected(0);
}
});
然后
ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
int currentPosition = 0;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int newPosition) {
FragmentLifeCycle fragmentToShow = (FragmentLifeCycle) adapter.getItem(newPosition);
fragmentToShow.onResumeFragment();
if(newPosition==0)
{
currentPosition = newPosition;
}else {
FragmentLifeCycle fragmentToHide = (FragmentLifeCycle) adapter.getItem(currentPosition);
fragmentToHide.onPauseFragment();
currentPosition = newPosition;
}
}
有一个界面
public interface FragmentLifeCycle {
void onPauseFragment();
void onResumeFragment();
}
在所有片段中实现接口。假设处理程序在位置0的Fragment中使用。
@Override
public void onPauseFragment() {
if(handler!=null)
handler.removeCallbacks(runnableCode);
}
@Override
public void onResumeFragment() {
handler = new Handler();
// Define the task to be run here
runnableCode = new Runnable() {
@Override
public void run() {
Log.i("Do ","Something");
handler.postDelayed(runnableCode, 3000);
}
};
handler.post(runnableCode);
}
在其他片段中,只需实现界面即可。