Cannot see the changes from database after adding in android? - android

I have tabs activities. one activity gets data from local database, but when I add information to database through another activity and back to the activity which shows the database contents, I don't see the changes! >> I need to re-run the applications to see the changes !
why and how to solve it ?

Ahh you probably need to re-bind your adapter, or call notifyDataSetChanged() on your adapter.
For instance, in your onResume() method call listView.setAdapter(new MySpecialAdapter()). That way no matter if your program is resumed from your other activity, or some other program, it will refresh the data.

Related

Android RecyclerView reloading the activity to update data vs notifyDataSetChanged efficiency?

I have an activity that in onCreate calls an API and populates a RecyclerView with product names, current stock, and an EditText to insert a value for each product.
You can then hit an add or subtract button at the top of the activity and all products with an inserted value will have their stock updated in the database by the given values via the API.
I do not pass the on screen stock values to the API (as another user may have changed it in the meantime), simply the inserted values, product ids and whether to increment/decrement the database value.
Currently, after I send the update request and it is successful, I call finish() and just reload the activity to get the updated data.
I'm wondering if it would be better to make an API call for the data after the update request and then just update the RecyclerView, it would almost be duplicate code of the onCreate but would prevent reloading the activity.
Does anyone have any insight as to the efficiency of this vs reloading the activity? It seems like reloading would be the more inefficient option but i'm not too sure.
Assume in your onCreate method you have something like this:
onCreate() {
setContentView(xx);
callApiGetDataAndSetDataToRecyclerView();
}
If so, pls try to change the code like below after I send the update request and it is successful:
doAfterUpdateRequestSuccessful() {
List<Object> latestData = callApiGetData() // this should not be in the ui thread, I believe you are clear about it but just want to repeat
recyclerViewAdapter.setData(latestData);
recyclerViewAdapter.notifyDataSetChanged();
}
Then your recycler view will be updated with latest data.
And like #ADM's comment, finish and recreate the activity is never a solution for updating recycler view, the ux will be very very bad.
Reload the activity is not so efficient. You should instead move your fetch data and render logic to an independent method and invoke this method from onCreate and every time you want to refresh the rendered data.

How to call the web service only once in android

I have three tabs in my view. Every time when I press the each tab some layout will be inflated and a web service call is done. I am populating some data into my inflated view through this web service call. Everything is working fine. But the problem is I don't want to call the web service every time when I press the tab for the second time. Previous data should be populated automatically without making a call to the web service. I don't think I can use onResume() method for this. Googled out many pages to find this answer but nothing is helpful. Please post the code if necessary. Thanks in advance.
These are my workarounds till now.
TabHost setCurrentTab only calls oncreate method for Activity in Tab once
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
http://jnastase.alner.net/archive/2010/10/27/handling-orientation-change-in-android.aspx
Your webservice should return some data, keep this data to your local memory and check every time either this data is null or not, if null then call service otherwise inflate from local data.
First of all, you should know the state of your 'call': not calling, pending, called. You must store it somewhere (activity, shared preferences, database etc) then making a call. Now, for data: you should implement some caching (again, in activity, shared preferences, database or even in plain files).
Then, in any state of a call, you can show data from cache, if it exists, and when call is done you can store new data in cache and show it.
whenever any changes made in one tab, store value in some variable, whenever next time the page is initialized, initialize with old selected value. Please try it out.. It may solve the problem..

Activity lifecycle - array was erased

I have a problem with Activity life cycle. In my server communication Activity I am downloading the list of items from the server and then setting up the Adapter for the ListView.
Everything is fine but if I press Home button on this screen and after some while (e.g. 3 hours or more) return back to the screen via application manager, application crashes. The problem is in the onTextChanged() method (which is usefull for searching via EditText) where I am calling the setAdapter() method again. There is nullPointerException because my array was somehow erased.
Why is the onTextChanged() method called again during restoring? And why was the array erased?
Thank you for your help.
Please check the activity lifecycle diagram on:
https://developer.android.com/reference/android/app/Activity.html
Your Activity goes to Pause state after you press Home.
And after 3hrs or even a shorter time,it may be killed by system for resource management.
So it need to be created again on next launch.
I think you should add code to handle onDestroy() and onStop().
Retrieve the data the same way you did originally, by overriding the
onResume()
method, checking if the data is existent or not beforehand.

When to load database data into an adapter

I have a simple personal application I'm working on that queries some records in an SQL Database and populates an adapter for a listview and is basically working fine... but I've began to wonder if I'm doing certain things at the right point of the framework.
Currently I'm loading everything up during onCreate(). In theory, I could be loading up quite a bit of data, so I wanted to possibly throw up a ProgressDialog while the information is being added to the adapter, but I ran into some odd threading issues with the Cursor. Ultimately, I launched a Progress Dialog near the end of onCreate(), followed by sleeping on another thread and calling a method to load my data with runOnUiThread() following the short sleep time, having the end of that method dismiss the Progress Dialog.
This works, but it's brought me to whether or not I should be loading database data during onCreate... or whether it should be moved to onStart() or onResume(), adding in code to clear the close and open the database, clear and repopulate the adapter as necessary as other Activity's are started and finished. Or would all that be unnecessary and I should just keep the adapter populated during onCreate()?
Reto Meier's suggestion to use an Application may suit your needs. Take a look at Activity restart on rotation Android
Move it to onResume, as if you stop the activity you can destroy the adapter and fill it back when to resume the activity.
It helps to save memory and also helps to update the adapter if data has changed.

How to put the proper code for each android's life cycle?

What is differences between onCreate and onStart cycle ?
I got confuse to put the proper code for these cycles.
for example in my case, I have main activity listview to display data from database and other activity to create data to database.
Activity to create data is called by listactivity. After creating data is succeeded, it would be back to listactivity. And the data in listview should be updated.
When I put initialization database, cursor and adapter on onCreate method, the listActivity wouldn't update the latest data after creation the data.
But if I put it on onStart method, it's updated.
my question: Is it correct in this case to put all initialization on OnStart method ?
Because I'm thinking it would be expensive to reinitialize for each database record to listview if there is one updated data.
What is differences between onCreate and onStart cycle ?
onCreate() is called when the activity is first created. onStart() is called whenever the activity becomes visible, which includes when it is first created (after onCreate()) and after it is coming back to the screen from being stopped (e.g., another activity took over the screen).
I have main activity listview to display data from database and other activity to create data to database. Activity to create data is called by listactivity. After creating data is succeeded, it would be back to listactivity. And the data in listview should be updated.
Use a managed Cursor, and that will happen automatically. See startManagingCursor() on Activity.
It depends. Do you want the data to be reloaded everytime the user comes back to the visible activity? If yes, you should add these methods in onStart(). If you are looking to load the values at the very beginning (more like a once off job) and do not want to be reloaded, then onCreate is the right place.
See doco for explanation of onCreate and onStart. This diagram in the link is useful
http://developer.android.com/guide/topics/fundamentals.html#actlife

Categories

Resources