"onWindowFocusChanged()" not excuted when switching fragments - android

I have an activity, with multiple master-detail fragments in the activity.
Fragment 1 is opened with a button click in Activity, and Fragment 2 is open with a button click in Fragment 1.
Activity => Fragment 1 => Fragment 2
As I know, onWindowFocusChanged() is called when Activity switch to Fragment 1.
However, it is not called when Fragment 1 switch to Fragment 2.
Is there any callback method invoked when switching Fragment 1 to Fragment 2 that I can use?
I need this call back so that I can render my Fragment 2 layout correctly as I added some TableRow with TextView programmatically in Fragment 2.
I tried using onCreateView(), onStart(), onCreate() methods in Fragment 2 but it doesn't work because the layout(TextView, TableRow) that I added programmatically in Fragment 2 isn't added to the RootView yet.
Any suggestion or solution?

Related

Android. How to avoid fragment be recreated when back from another fragment?

I'm using MVP in my project. I have an activity. Inside this activity I'm showing fragment. Fragment contains some data, edit text, buttons etc. From this fragment it is possible to navigate to another fragment. Here is my code for showing another fragment:
getParentFragmentManager()
.beginTransaction()
.replace(R.id.main_container, secondFragment)
.addToBackStack(null)
.commitAllowingStateLoss();
Next, when I try to go back from fragment 2 to fragment 1, my fragment one is re-created.The method onViewCreated() is called again with saveedInstanceState = null. Also in the onViewCreated() I call presenter.onCreate(savedInstanceState) and because of this, all requests that should be called when I first enter the fragment are re-called when I back to first fragment.
As far as I understand, when calling a .replace(container, fragment), I cannot avoid recreating the fragment view, but in this case, how can I save all my data in the presenter so that I do not re-execute all requests when returning to the fragment?
P.S. With .add() fragment is not recreated. But in my activity I have toolbar with title, and I don't need it to show in my second fragment. When I open my second fragment with .replace() toolbar is not showing, but when open with .add() toolbar is showing.
Use Fragment Result API :
For implemention this method set - setFragmentResultListener - in source fragment and also set - setResult() and finish() - in destination fragment
Note 1 : for having clean code i suggest you to use FragmentTransaction class in:
https://github.com/mahditavakoli1312/FragmentTransaction---mahdi-tavakoli
Note 2 : use Navigation for navigate betwin fragments and activities Instead tranastion

FragmentStatePagerAdapter does not retain its state when back to parent fragment

I'm making a view whose architecture is:
A fragment has view pager that has FragmentStatePagerAdapter(adapter 1), FragmentStatePagerAdapter (adapter 1) returns a Fragment when click on Fragment it opens another Fragment that again has a ViewPager and FragmentStatePagerAdapter(adapter 2) attached to it that again returns a Fragment.
My problem is when I'm clicking on Fragment and returning by pressing back button the Fragment is blank, also when I'm using getChildFragmentManager on adapter 1, application crashing that no view found for Fragment.
Edit:
by debugging I found that FragmentStatePagerAdapter wont return fragment that was returned previously thats why I'm getting blank page
In a fragment:
viewPagerDetail.setAdapter(new ProductDetailViewPagerAdapter(getFragmentManager()));
tabLayoutDetail.setupWithViewPager(viewPagerDetail);
(if I'm using getChiildFragmentManager here it crashing application)
Inside ProductDetailViewPagerAdapter:
#Override
public Fragment getItem(int position) {
return new ViewPagerImageFragment();
}
Inside ViewPagerImageFragment: there is single imageview when I click on image view it opens a fragment that has viewpager that has adapter attach on it that also returns a fragment when I press back to fragment that has ProductDetailViewPagerAdapter the viewpager is blank
For the fragment like these kind of nested, we should see for hierarchy of the fragment either we want to see parallel or step ahead or below for step ahead or below choose getFragment manager when replacing and
getParentFragment().getFragmentManager()
for parallel fragment replacement
Happy coding!

Receive a unique intent/bundle value in activity

I have a
1) MainActivity.
2) Fragment 1 (Listview)
3) Fragment 2 (Listview)
4) Fragment 3 (Textview)
I can successfully launch Fragment 1 when the app is first started.
Then, once i click on any listitem on Fragment 1, Fragment 2 is launched. I've handled both these fragments with the following condition in order to differentiate inside my MainActivity
if (this.getIntent().getExtras()
== null) {
fragmentManager.beginTransaction()
.replace(R.id.restaurantlist, Fragment1).commit();
} else {
String id = String.valueOf(getIntent().getExtras().getString(
"restaurant_id"));
fragmentManager.beginTransaction()
.replace(R.id.offerlist, Fragment2).commit();
}
Now how do i move to Fragment 3 by clicking a list item on Fragment 2?
Right now, im also using putEctra AND getExtra "id". For Fragment 2 and Fragment 3. But how do i use it to differentiate inside the above code?
Your question is not clear enough for me. If i get you right you have 3 fragments and there is a situation you want to show them all at the same time (e.g. in tablets). If that is not the case don't use fragments at least the way you are using it right now, Instead use another activity because using fragments where there is no need for them only makes your application more complicated.
Any way i suppose you have 3 container in your activity. when app starts add them all! . also add a onItemClike listener to fragment 1, and fragment 2. so when ever a click on item happens you now about that. Now whenever a click happens inform the other fragment to show the relevant item. to do that you only need to define a public method in fragment 2 and fragment 3.
you can also use fragment manager show() and hide() methods to hide your fragment when necessary.

Call Fragments onCreateView in context with Tabs

I have 2 Tabs and 2 Corresponding Fragments. On calling the LAUNCH Activity both Tabs were added and then the first one added will be shown. Thus the first Fragments onCreateView is called the second Fragments ones not.
In my case this is an issue because the first Fragment has to call methods on the second Fragment. Inside the second Fragment there is an Objectreference which will be set by calling the onCreateView in the second Fragment.
Therefore I used following code snippet to solve this
actionBar.setSelectedNavigationItem(1);
actionBar.setSelectedNavigationItem(0);
It works but in my opinion there must be another possibility to solve this issue. Like calling the onCreateView of the second Fragment?
Here is the relevant code snippet. The listener is implemented as in android-dev Sample only with small changes not affecting my issue.
simplexFragment corresponds to the first Fragment
graphicFragment corresponds to the second Fragment
// adds two tabs
actionBar.addTab(actionBar.newTab().setText("Input").setTabListener(new TabListener(null, "input_fragment")));
graphicFragment = new GraphicFragment();
actionBar.addTab(actionBar.newTab().setText("Graphic").setTabListener(new TabListener(graphicFragment, "graphic_fragment")));
simplexFragment.setGraphics(graphicFragment); // sets the internal reference!
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// selects the Tab so the GraphicFragments onCreateView will be called
actionBar.setSelectedNavigationItem(1);
actionBar.setSelectedNavigationItem(0);
Thanks for support.
In my case this is an issue because the first Fragment has to call methods on the second Fragment.
This is not how Fragments are meant to work. The idea is a Fragment should be self-contained and re-usable and one Fragment shouldn't know that another exists (or at least shouldn't rely the existence any other Fragment).
For example, suppose you have 3 Fragments and ActivityA uses FragmentA and FragmentB but you have another Activity (ActivityB) which uses FragmentA and FragmentC. In that case, FragmentA doesn't know what the other Fragment is (B or C) and shouldn't even expect there to be another Fragment at all.
The way to achieve what you want is to use callback methods on the Activity and have it perform the actions on any other Fragments (if they exist). It can do this by either calling public methods on the other Fragments or by passing data in the 'arguments' Bundle when it creates the other Fragments.

ViewPager with Fragment reload at first and last page

I'm trying to create a ViewPager with six fragments but only 2nd fragment to 5th fragment contain data that I want to show and the first fragment and the last fragment I want to be used to reload the data and set the position to the 2nd fragment again. The overall flow is like this :
1st (reload and go back to 2nd) <- 2nd fragment <-> 5th fragment -> 6th fragment (same with 1st)
what I've tried is I create a callback from the 1st fragment and 6th fragment like this
public static class callbackFragmentLoading implements callbackFragmentLoad {
#Override
public void onLoading() {
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(2,false);
}
}
and I passed the callback to the fragment constructor so I can called the onLoading function in the onActivityCreated. But I everytime I do it the application will be force closed and the logcat shows
recursive entry to executependingtransactions
is there any way to do this? or my method for doing it is wrong?
Thank You
is there any way to do this? or my method for doing it is wrong?
Messing with callbacks between Fragments of a ViewPager isn't probably such a good idea. Instead I would do it like this:
Don't load any data(like with a Loader) in the Fragments from the ViewPager, instead let the FragmentActivity do it(and the Fragments will get it through methods from the Activity).
Your two loading fragments(position 0 and 5) will call in their onResume method a reload action on the parent Activity(like a Loader restart)
At this moment the Activity will load/reload the data and when that finishes it will set the ViewPager to the correct items(either 1 or 4)
in the onResume method of the data fragments you'll refresh the fragment's data(here you may need to use some sort of signaling system because you'll need to duplicate the refresh code in the onCreateView(some fragments may have their view destroyed if they are far apart from the current visible position)).
As I don't know many things about the inner data fragment I've written a basic skeleton sample(without the data loading in the activity).

Categories

Resources