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?
onResume() is called whenever you navigate back to the activity from a call or something else. You can override the onResume method similarly as onCreate() and perform the task. This may help you understand the lifecycle of and Android app more.
The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView() . Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.
onCreateView() : The system calls this callback when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
The Fragment class has two callback methods, onAttach() and onDetach() , that you can override to perform work when either of these events occur.
Code follows:
Step: 1
Create Interface:
public interface YourFragmentInterface { void fragmentBecameVisible(); }
Step: 2
Attach Listner In 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) { } });
Step: 3
Implement Interface In Your Fragment:
public class yourActivity extends SherlockFragment implements YourFragmentInterface{ @Override public void fragmentBecameVisible() { System.out.println("TestFragment"); } }
Implement this listner in all yor fragment activity fragmentBecameVisible()
this method call during Fragment switching.
Thanks.
You can simply simulate onResume() for each fragment by using an interface!!!
1- create an interface named FragmentLifecycle , the code is as follows:
public interface FragmentLifecycle { public void onResumeFragment(); }
2- Let each Fragment implement the interface:
public class FragmentOne extends Fragment implements FragmentLifecycle
3- Implement interface methods in each fragment:
@Override public void onResumeFragment() { Log.i(TAG, "onResumeFragment()"); Toast.makeText(getActivity(), "onResumeFragment():" + TAG,Toast.LENGTH_SHORT).show(); }
4- Call interface methods on ViewPager page change:
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { FragmentLifecycle fragmentToShow = (FragmentLifecycle)adapter.getItem(position); fragmentToShow.onResumeFragment(); } });
Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With