Fragments inside ViewPager initializes itself onBackPress - android

I can't show you the code, because first of all the code is too long and also I will do everything to describe you the problem to understand it easily , as clear as I can.
So In my app I have one activity (That's the MainActivity). Inside this activity I initialize a fragment. In this fragment I have ViewPager. In that ViewPager I have 2 fragments (Fragment_A, Fragment_B). When the app opens, it creates the ViewPager, makes a simple request to server and if the response is successful, it creates Fragment_A and Fragment_B. Everything is fine. Next step. When I click on Fragment_A or Fragment_B, it opens new Fragment (Fragment_C) and adds the ViewPager into BackStack. When I click back button on phone it has to load the ViewPager, like when the app starts (Create ViewPager, make request and on successful response create Fragment_A and Fragment_B). But watch here what's going.
1) It creates the ViewPager and makes request.
2) When the request is going, without waiting for the response it creates Fragment_A and Fragment_B (but as you remember it creates the fragment only when comes successful response).
3) After some time when ViewPager comes the response it creates Fragment_A and Fragment_B again.
So that means that it creates Fragment_A and Fragment_B second time.
I think you understood.
How to avoid this? Why does it initializes Fragment_A and Fragment_B without waiting the response? This problem occurs only when I click back on phone. But when I open the app first time, all works fine. Thank you.

Related

Sharing data within Fragments in Tabs using ViewPager

I'm trying to send data to 3 other fragments in a Tab of 4 from the first Tab which gets the data from the server, but observing the creation of the Tabbed Fragments, First and Second is Initialized, so the second doesn't get the data and the third and fourth get it. Even, when I tried to load the data from onResume in the second Fragment Is there anyway I can let tell the Second Fragment should reload itself when the Tab is clicked. Any help would be appreciated
Maybe using OnPageChangeListener would when you detect when the SecondFragment becomes active.
I would generally recommend notifying such things through the parent Activity

FragmentStatePagerAdapter fails to load the fragments again when I get back to old instance of the parent Fragment

Scenario:
I have a fragment which has a ViewPager which contains 5 instances(different) of a single Fragment with different values, each having a listivew that contains some sort of items.
Problem:
The flow goes smooth when I click an item of listView(say on page1) but the moment I come back from there on pressing back (overridden OnBackPressed in the main_activity (will discuss later)), the viewPager fails to load the subFragments.
But when I switch to the 2nd or 3rd page, it gets displayed, and on going back to 1st page, it now gets displayed.
OnBackPressed():
I am maintaining a manual stack here. When I click on an item of ListView, the current Fragment Instance (of the parent Fragment ofcourse) goes into stack. And when user comes back , that instance gets popped off the stack and I replaces it on the activities FrameLayout container.
References:
Fragments not getting recreated by FragmentStatePagerAdapter after coming back to the main Fragment instance
Guys, I am really pissed off here, please help me out.
Frankly, I haven't worked much with fragments.
But it seems like the page is being thrown off the memory every time you switch to other pages. I am not sure but if there is a way for android to manage this and keep the page in its memory persistently, it might solve the problem
Finally I am able to get a workaround for this issue, which included two things:
while replacing the fragment make a call to
ft.replace(R.id.content_frame, frag).addToBackStack(null);
while initiating the ViewPager, the fragment manager to be used is :
mAdapter = new myAdapter(getChildFragmentManager());
Actually the problem is that, android fails to recognise and load the older instance of the fragment unless you explicitly add that instance to the backstack. So after adding it to BackStack, there is practically no need to maintain your own manual Stack, but you still can do that.

Resume Issue in ViewPager with Tabs

I am having an issue in ViewPagerwith Tabshaving Fragments. I have my Home Activity that has a ViewPager with Tabs. For each Tab there is a Fragment. There are 3 sets of data coming from service when we start the application. The requirement is when I do changes for these sets of data in some other activities, I should be able to see the changes in the Home Activity, that means the service calls are to be done again in each of these Fragments, which is why I am doing all this part in onResume().
Required Output is :
When coming back to HomeActivity(that has the tabs) from another child activity, the service calls should be done.
When hitting on Load More button in any of the tab, the service call associated with that fragment or Tab should be done.
When moving between the Tabs, service calls should not be done, instead the data has to be loaded from Database.
Current Output is :
Since I kept all the service calls for those Tabs in onResume() of the associated Fragment, the first and second requirement is met.
When it comes to third requirement, the output is same as the first one i.e., its making service calls which should not happen.
Problem : How to identify if user is moving between the Tabs or if he is coming from child activity. This service calls happening while moving between Tabs is causing performance issues. Please help me with this, I have no idea about how to proceed to overcome this performance issue. Thanks.

Going back to my main activity, and refreshing the fragments

So, my application have one activity ProjectActivity as a home, which contains 3 fragments HomeFragment ListFragment TagFragment(one for each tab). When I click on a button (on the homeFragment), it opens a new activity CaptureActivity.
That activity launches an ASyncTask, ParseData.
On the ParseData onPostExecute, I would like to go back to my ProjectAcitity, focused on a specified tab (the one that contain ListFragment), and update the ListFragment View.
I actually have no clue on how to do that. Do I simply need to call the ProjectActivity ? But then, if I press the Back button, wouldn't it go back through the whole chain (Project Activity, CaptureActivity, ProjectActivity) ?
Well, you can do a few things.
First just have ListFragment monitor w/e its listing and refresh it in its onStart/onResume which will be called when you return to your ProjectActivity.
Or more directly you could do startActivityForResult(captureActivity); then when u come back to your ProjectActivity get results in onActivityResults() then update your ListFragment from there.

How can I “refresh” the fragments in a ViewPager?

I am having an issue here that is driving me nuts. What I have is the following:
FragmentActivity1 holds the viewPager. It instantiates the FragmentAdapter and two fragments and attach them to the ViewPager.
Fragment1 has only one button. When user clicks this button, I create one Intent and invoke a FragmentActivty. This fragmentActivity contains a form, which the user fills in and press OK. When he does that, I persist the data in the DB of the app.
Fragment2 is a ListFragment, and lists all the data that was inserted previously by the user.
By the time user completes the form and presses OK, I persist the data, like I said and finish() the activity, returning to Fragment1. WHen I swype to Fragment2, the data is not there. I need then to swype to Frag1 and then back to Frag2. Only then I can see the problem.
I have tried setting listeners between activities and fragments but, still, cant make this work.
Have anyone seen? I am willing to share my small project as well, so you guys can take a look.
Thanks,
Felipe
I usually fill my ListView in onResume() in the ListFragment. When I'm done adding new stuff to the list (In a seperate FragmentActivity, just like you), the ListView automatically gets refreshed, because onResume() gets called ;)
Another approach is using onActivityResult(), then you can update list only when something new is added or something is removed

Resources