I build an Android App with 3 Tabs. Now I want to call a method, when I switch between this 3 fragments to update the view of the fragments. But the onResume() method doesn't work for that. Do you have any ideas how to call a method in a fragment, when switching to it ?
Thank you.
If you are using “import android.app.Fragment;”
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.<specific_function_name>();
Related
I am using 4 fragments in 1 activity. The navigation is done via NavDrawer. Every time I navigate through the fragments, each fragment goes through the following methods:
onAttach() to onDestroyView()
I was expecting them to go through these once and then go through onCreateView() to onDestroyView() but this is not happening.
I would like to initialize certain things only once in onCreate() but now I cannot.
Does anybody have any experience with this?
Hi Thanks for the reply.
I found the problem. I was creating a new fragment in the nav drawers itemselect. To solve it I created an instance of each fragment in main activity and just set this to current fragment instead of creating a new one.
Thanks again
I have one Activity with a viewpager in it and also a frameLayout which is used as a container for fragments that should be drawn above this viewpager.
When I click a button of my Activity a fragment is shown in that framelayout container. On a press in the navigation bar I can close this fragment. All these things work fine when called from the activity itself.
I have now the situation that on a specific action from one of the fragments in the viewpager I invoke a callback which the main activity implements and then shows a fragment in the frame layout.
But here now comes the problem. When I want to navigate back or rather want to close the fragment like I described before, nothing happens.
So in conclusion:
"Fragment show to container" invoked from Activity directly -> I can navigate back from the fragment by getActivity().onBackpressed()
"Fragment show to container" invoke from fragment in viewpager and then delegated to activity -> The navigation back or getActivity().onBackpressed() has no effect.
So my question is: How can I close the fragment in the container even when I call it from one the fragments inside the viewpager of the activity?
Probably you are adding a fragment to the backstack, so onBackPressed you should pop last fragment from the backstack.
Something like that:
FragmentManager fm = activity.getFragmentManager();
fm.popBackStackImmediate();
Meanwhile I found the problem..
Due to the fact that I am running some kind of update loop, I added the fragments multiple times to the backstack but didn't realize it because the fragment itself didn't change, the new instances just got added to the backstack. So when I called the onBackpressed() method the instances got removed from the backstack but because there were like 200 of them nothing happened.
I'm creating android app that have 3 fragments and I want refresh data every time I come back to the fragment. So, I override onResume() method in every fragment and add system out print to onresume to check if it's worked correctly.
But when I navigate to 2nd fragment it shows the add system out print of fragment 3 onresume. when I go to fragment 3 it not showing any add system out print. but when I came back to 2nd again it shows add system out print of fragment 1.
Please help me to fix this issue.
It appears you are using FragmentStatePagerAdapter in your ViewPager. It is the expected behaviour of the adapter that only neighbouring fragments are created. If you do not want this behaviour use FragmentPagerAdapter. But be aware of the memory taken up by all the fragments.
Make your fragments implement an interface:
public interface Listener {
void resume()
}
Make your activity implement OnPageChangeListener:
viewPager.addOnPageChangeListener(this);
and then do the following:
#Override
public void onPageSelected(int position) {
((Listener) mAdapter.getItem(position)).resume();
}
I'm trying to open a new fragment based on a button push in a previous fragment. What's the best way to implement this?
I'm curious if it's Activity -> .add + .commit original fragment - > from that fragment.java .replace new fragment?
Or do I need to pass an intent back up to the activity and create/replace that fragment from the activity?
So summarize: Activity A - > Fragment 1 - > Fragment 2.
I'm also slightly confused on what things I [need] to #Override. I think only onCreate and onCreateView [within each fragment]?
I'm only looking for high-level here; I want to struggle through the code myself.
Fragments are generally unaware of their host so I would use the standard callback method to call your activity and ask it to switch fragments.
Create an interface
Have your activity implement the interface.
Cast the getActivity() call to your interface.
Call the interface method.
This is much cleaner than casting the host activity and calling methods on it. It also means your fragment can be hosted in different activities with no cast errors.
http://developer.android.com/training/basics/fragments/communicating.html
You need to cast getActivity like:
((MyActivity) getActivity())
And then if you have that, you can call a method in yout activity:
((MyActivity) getActivity()).replaceFragments(Object... params);
And inside the method you should do the replace fragment process.
So simply you have the right idea.
I'm looking for a method in the Fragment Lifecycle, but I'm not sure which one.
Here's my situation: I've got a Fragment inside a ViewPager. The Fragment displays a List with some information. I fill the list in the Fragment's onCreateView(). When the user opens a different Activity (settings in this case) and changes some settings, the information that the List in the Fragment has to show, changes. When the user returns to the Fragment using the Back-button, the onCreateView() isn't re-called, so the information in the List isn't updated.
My question is: The onCreateView()-method isn't called when the user returns to the fragment form a different Activity, but which method is called here? I need to know this because then I can fill the List in that method.
Thanks in advance!
Important and non-obvious point it that Fragment's onCreateView() being called not only in case you selected Tab with this Fragment. So don't rely on onCreateView() of Fragment when using ViewPager(). When You select the Tab, Android creates sible views (caches them) or makes something similar.
You should call your update method when user selects proper Tab in ViewPager (don't remember exactly, but hope it helps).
onResume() is the simple answer, called when user comes back. for more details refer lifecycle here FragmentLifecycle