Fragment View not updating - android

The issue i am running in to is something i have tried to search for continuously.
I have a fragment(FRAGMENT A) that I use to load custom views in too. Each of those views contain textviews which are populated by running an asynctask to get values. Now I have a button that i use to load a new fragment(FRAGMENT B) which allows me to edit one of those values in my custom views and then it calls popbackstack going back to (FRAGMENT A). onResume(Fragment A).
I rerun the async task to get the new values but it does not update.
I have tried multiple things but it does not seem to work. Please help.

The way I got around that was to simply put a small delay before running the get values procedure after the FRAGMENT B comes back to FRAGMENT A.

Related

Changing layout files for a fragment during runtime

I am trying to develop an app in which fragments are involved. There are 2 fragments on screen. The list fragment containing a Start and Stop button and a detail fragment on the right side. On click of the Start button an audio processing code runs in the detail fragment. So depending on the result i get out of the process I want to change the layout of the fragment. Basically I want to change the layout file of a single fragment during run time depending on the results I get while doing some process. How can I achieve it?
Thanks!!
You cannot switch to a different layout file in a fragment, the view you returned from onCreateView cannot be replaced.
However, you have several options:
You can show and hide views at runtime with View.setVisibility().
ViewStubs can be inflated at a later time.
Or you could replace your detail fragment with a new fragment.

Android veiwpager with fragments view flow issues

Im relatively new to android so im trying my hand at what i though would be an easy-ish app but I've ran into an issue to do with view/activity flow that i cant get an understanding on.
i have a fragmentActivity that uses a Viewpager to create tabs, each of those tabs is its own fragment class, thats all fine and working, but now i need to have one of the tabs display a list, when selected it takes you to another "view", my problem is how to create the first list and then how to handle tha clicking of an item in that list to take you to the new view so that the tabs stay in place and the back button doesn't exit the app.
currently ive swapped out the fragment with a list fragment that uses an arrayAdapter to build itself, this has worked as far as the list goes but i cant for the life of me figure out how to utelise its onclick() method to move on the the next screen, without as i said losing the tabs or having the back button simply exit.
so im not sure if A, the list fragment is the way to go, or B if it is how to move on to a new screen correctly
i can post code if needed but its a very general implementation of the classes mentioned so im not sure code will help
The callback you want to handle a click on an item in the list is onListItemClick.
As far as presenting a new screen, you can use a FragmentTransaction to replace your fragment with a new one, which gets a little hairy if you're doing this inside of a ViewPager. The code would look something like this:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.some_containging_view, new SomeFragment())
.addToBackStack(null).commit();
Otherwise, you could simply launch a new Activity, which is simpler, but will mean that the tabs won't be present on the new screen unless you duplicate them there.
An elegant solution was finally found here, it uses a wrapper fragment around my list fragment so the pager is just concerned with the wrapper, meanwhile within the wrapper i can perform fragment transactions to my hearts desire, one small note the linked code uses getChildFragmentManager() that needs to be changed to regular fragmentmanager() from support.app for the backstack stuff to work.

Android Fragment Active on Opening

I am using ViewPager. I have 3 fragments (A,B,C) and two of them(A & C) are populating ListView from the server, so it will take a few seconds for that. And I am showing a ProgressDialog. B is the fragment which is set to show first and its is not having any background process, having a few buttons only.
So the problem is that the ProgressDialog will always show upon creating that activity,that is all the Fragments are loaded always.
So I wonder if I can load the specific Fragment only (A or C) when I open it (Slide) so that the user who want to use the Icon menu (B fragment) should not need to wait for the other fragments (A & C) to load. I am not sure whether its possible or not. Can anyone suggest me any code snippets or references to achieve it. Any help regarding this will be really appreciated.
Nevermind, I had done it easily with AsyncTask and the Fragments are updated in background

Why is my fragment onCreate being called extensively whenever I page through my applications viewPager?

I'm not quite understanding this fragment lifecycle business.
I have a pretty standard 3 page horizontal slider view Pager layout for a "view details" section of my app. I start my app on the middle page of the three. My FragmentActivity sets the page to page 1.
mPager.setCurrentItem(1); //default to center page, current data view fragment
I've been using the FragmentStatePagerAdapter because using the FragmentPagerAdapter crashed my app at times, when coming back from a suspended state for example, and this was the quickest way to work around that for now. Seems to work but I suspect the State aspect is what might be the cause of my problem here, maybe.
So at first I thought that I would have each fragment do the work of getting data from a rest service and then showing it in a list, but I'm not so sure any more.
I tried running a unique async task to fetch data in each of the fragments onCreateView events.
Then after reading more on the fragment lifecycle I switched to onCreate, having noticed that the onCreateView was being called quite heavily, which in turn made for a greedy app that too often requested data over the wire.
Switching to onCreate hasn't changed anything. OnCreate is still geting called just as much as onCreateView for the 2 adjacent fragments.
The odd thing is that the fragment that I set to be the first one to display in Fragment Activity only gets the onCreate called the one time.
Something doesn't feel right here.
Right now I'm thinking that I should have the parent fragment activity declare and call all the async tasks to fetch the data that I need to display in the fragments.
Set the results of the async calls in an object owned by the parent fragment activity and then have the fragments use the object contained by the parent to create the list view etc.
But what if the async tasks started by the parent activity don't finish before each fragments onCreateView is called and the object contained by the parent isn't ready yet.....
signed, confused and frustrated
ViewPager is quite zealous in shutting down things it isn't currently using, and this is exactly what is happening here. The default behaviour is for ViewPager to "keep around" one page either side of the page being viewed, and destroy the rest. Hence in your 3-page view, page 3 gets destroyed when selecting page 1, then when page 2 is reselected page 3 is recreated. As you've noticed, page 2 only has onCreate(..) called once because it is always adjacent to, or is, the currently selected page.
To solve this, simply set ViewPager.setOffscreenPageLimit(2). Then the ViewPager will keep all your Fragments. Obviously this isn't a good idea for a large number of Fragments, but for your case it should be fine.
#Espiandev's solution will work for your current case, but you're right that your state is the issue. You should use the setArgument and/or onSaveInstanceState methods to save your Fragment's state (which shouldn't be too hard, since e.g., a response from the server can usually be represented as a String), and then use getArgument and/or the Bundle passed in onCreate to restore it.
Alternatively, you could have your Activity do the server fetches, and then call setArgument for each of your fragments, and check the arguments inside your Fragment to determine if your data has arrived yet (and if not, potentially display a loading state of some kind).
If you care at all about screen orientation change, this related question will also be useful to you.

views in tab not getting displayed after database update

I am using two tabs in my android application.
In Tab A, the user enters some values and a database is updated based on the entered values.
In Tab B, I am trying to display the updated values of the database using TextViews.
But when I switch from Tab A to Tab B after entering the values in Tab A (i.e. after updating the database), the Tab B appears empty.
Only when I restart the app, the Tab B appears correctly with updated values.
Please suggest to me what could be the problem...
In Tab B's Activity you should put code that loads data from database and displays it, inside .onResume() method.
Check Activity Lifecycle document to see what lifecycle methods are run at what lifecycle stages.
Posting your code could be helpful as we can't tell from your post if you are using tab activities or tab view, but either way it sounds as if you aren't refreshing the data. You need to figure out the correct event and actually reload the text of the TextViews. When you are restarting your application, it is loading everything from the database, and so you see all the correct data, but when you update the data, all the widgets have already been populated, and there isn't anything that tells them that the database actually changed.

Categories

Resources