Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for the onResume() during Fragment switching

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?

like image 440
Manjunath Avatar asked Jul 24 '13 22:07

Manjunath


People also ask

What is the use of onResume () callback method?

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.

What are the differences between onCreate () onCreateView () and onActivityCreated () in fragments and what would they each be used for?

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.

What does the onCreateView () method return if a fragment doesn't have any UI Mcq?

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.

What are the methods you can override in the fragment class?

The Fragment class has two callback methods, onAttach() and onDetach() , that you can override to perform work when either of these events occur.


2 Answers

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.

like image 85
Dhaval Avatar answered Sep 28 '22 09:09

Dhaval


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!

like image 42
farhad.kargaran Avatar answered Sep 28 '22 10:09

farhad.kargaran