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.
Related
I have 3 tabs in my application.
In my first tab I need to read JSON data from URL and using the user location. It takes about 2-3 seconds to load the page.
My problem starts when I implemented tabs, and now every time im swiping next and back, the info is re-fetched.
I tried the following solution:
I moved my method to the MainActivity, and created a variable named "isUpdated" and updated it to 1 if the info has been fetched successfully. since then, the tab data is gone after swiping and it shows blank empty tab.
I understand that the tab data is wiped after swiping to another tab, but I need to save the tab content.
How I can save the tab information and show it after the user swiped to the next tabs, without re-fetching the data over and over again?
if you are using ViewPager then set your off screen page limit to something different
for example
mViewPager.setOffscreenPageLimit(2);
should be enough for 3 tabs
I have an application with two activities the launcher activity has two swipe tabs and one of those uses the a simpleCursorAdapter and a cursorloader to populate the views from a database.
The code works fine when I am using a single activity and without the tabs. I mean I get updates data as the the database changes, but in this new design I only get updated data only after I restart the app.
Here is the scenario after the application starts I initiate the the loader and populate the listview in the tab , then I can start the second activity by clicking on one of the item in in the listview, the main activity that I can close by pressing on a button.
after closing the second activity I get back to the main activity and insert a row in my database ( and up to this point every thing is working fine , I can see that in the debugger)
But the problem is that I wont get the newly entered item on my screen unless I restart the app.
Even I swipe several times between my tabs nothing happens.
I have tried to restart my loader in my fragment callbacks but still no changes.
What is going wrong in my design?
I have created a tabbed android application using android.support.v4.view.PagerAdapter.
There are about seven tabs in the application and I plan to add more. Each tab contains a lot of controls (TextView, Spinners, Toggle buttons, Check boxes etc.)
On the first tab there is a drop down to select a profile. The user can 'Load' or 'Save' a profile he wants.
A profile contains data for all the controls in the tabs and I need to update the UI controls in all the tabs.
I have all the data loaded from the profile but the UI controls are not getting updated.
There is a 'UpdateUI' function which calls 'set' functions (setText, setChecked etc. for individual controls after finding its view by ID).
I was informed that only three tabs (Previous, Current and Next) are kept in memory so I wrote the application such that the 'UpdateUI' function is called to set UI data only when user swipes to that particular tab (figuring out the active fragment).
Using DDMS logs I saw that the data loaded was proper but the 'setText' or 'setXXXXX' function does not update the fragment tab.
I have also checked several possible issues:
Possibility of 'OnTextChanged' or an 'OnListener' updating the data again.
Made sure 'UpdateUI' is called from UI thread.
Using notify data change and redrawing UI to make sure UI is updated.
I am a novice Java/Android programmer. Please point me in the right direction.
viewpager.setOffscreenPageLimit(size);
you can instantiate all your fragments by setting limit then you can update all widgets inside other fragments.
If my understanding of Android Fragment management is right, once the fragment becomes invisible for user (say some other fragment completely overlayed it or in your case you change tabs) Fragment goes through onPause and possible onStop lifecycle stages, this mean it's kind of hybernated and can't be changed before it get's visible. viewpager.setOffsetPageLimit(size) tells the fragment manager (or rather pageAdapter) how many fragments should be kept hybernated, and I doubt it playing with this will change anything, but let me know if it's, because otherwise the solution may be more complicated.
What I'd do is recreate a fragment every time user gets to see it and pass your profile data to it's constructor (or following better practice newInstance() static method), it will in fact save memory since keeping many fragments there may be overwhelming. Alternatively you can check what profile is chosen everytime fragment is calling it's onResume, and update your controls there.
Good day,
I make my test application in Android and find some difficult moments to do by myself.
I have two Tab Fragments and Host Activity (that creates two tab fragments) with Action Bar (actionbarsherlock). One Fragment allows to add some information about product and persist it into SQLite. Other Fragment has a listview that displays all the products from database.
The problem is that in the first tab I persist data in different thread with 5 second delay, so you click "Add Product to Database" button and after 5 seconds it will be added. But right after clicking I can switch to another tab with listview and the situation is as follows: data from the first fragment is not persisted yet because of delay, but after it is persisted, nothing changes in listview - the listview doesn't know about changes. I need listview updated authomatically when it notices there are changes in database. How can I do this? I use ArrayAdapter to populate data into listview.
I think you need necessary use broadcast receiver, and in it call method refreshDrawableState() for you list. Respectively receiver is your activity.
I'm a newbie Android developer and I'm trying to create an Android application with 2 tabs, using Activities for each tab. This is basically how it should work:
Tab A has 1 button
Tab B has 1 ListView
When the user clicks on the button in Tab A the application must put a value in the ListView in Tab B
My question is: how can I update the ListView in Tab B when I click on the button in Tab A? I know how to put values in a ListView when it's on the same tab where the button is located but my approach doesn't work when the ListView is in a different tab. I tried...
ListView myListInTabB = (ListView)findViewById(R.id.list_on_tabB);
but didn't work :-/
Any suggestion?
Thanks in advance.
Im not sure you actually need seperate activites for this, unless you are doing some specific work between your activities. Most tab solutions you see in Android solutions work simply by hiding the Views that are not related to the current shown tab. You will find the "visibility" property helpful for making this happen. Also, making sure all of your Views are in the same Activity lets you access their objects all the time, regardless of their visibility.
You don't do that. Just ensure the underlying Adapter has the latest data. Calling requery() on the Cursor does the trick, it automatically updates the ListView attached to it.
Edit: use notifyDataSetChanged() to let the list know that the data is stale.