Android, xamarin: Load RecyclerView when Activity is already visible - android

In my activtiy, I am loading a recyclerview that downloads pictures from the internet.
The only problem is, that the acitvity will only become visible when the loading of the pictures is done. I call the function from within the OnCreate() method. Since this made it obvious, I decided to put the function into OnWindowFocusChanged(), yet still the activity will only start showing when the pictures are loaded. (This is like a 1 second delay, but its a litttle too much.)
Where would I call my InitRecView() method to make sure it will start loading once the activity is already visible to the user?
Thanks

You are getting this problem because you are loading images on main thread. It may also cause skipping layouts.
Here is the solution: Use Handler or Async task for download images, show progress till then and once image is downloaded then notify adapter using adapter.notifydatasetchanged().
note:make sure you write UI related code in runOnUiThread since you cannot handle UI elements in background task.

Related

android activity with list loaded from parse.com

Say I have main activity that contains a list (never mind if it is a ListView or RecyclerView). Each time the activity is created it has in its onResume method a query for data from parse. The thing is the parse query I use calls findInBackground method so my list in main activity is always zero since by the the time the activity is constructed the query hasn't finish yet. when I use find (and not find in background) I see in android studio logcat a sentence like the follwoing : "to many operation on main thread".
What do u think I should do?
Thank u in advance
Expansive operations like this should be done on a seperate thread in the background, so the main (animation) thread will not be blocked, causing your app to lag. The findInBackground() method will do the same as find(), but on a different thread 'in the background'. When the job is done, it will call the FindCallback and pass the result so you can update your list.
Untill this is done you could show a intermediate ProgressBar in front of your empty list to indicate that the data is loading.

Android, show activity, download image and show on imageview (in that order)

I'm trying to solve this, right now I have a Details Page activity (which loads dinamically from the DB depending on the item that was selected from a ListView). Each Details page can have between 1 and 5 images, which are downloaded from an online server. The problem I have is that the activity is not showing until the pictures were downloaded.
Basically the activity won't show until onCreate finishes. I'm using a thread to download the image so the UI doesnt freeze, My question is, where could I download and set the ImageView to the downlaoded bitmap so the user will see the activity and information straight away after clicking on the list item and then download and set the image after downloaded?.
I tried putting the thread on the onResume but I think it doesnt help either. What would be the best way?
Regards,
George
You need to implement an AsyncTask. Which executes on a background thread and prevents blocking of the main thread. You simply create a class that extends AsyncTask like below
EDIT - Yes, this will display your Activity even before the images get downloaded. You simply call the execute() method of your AsyncTask in onCreate() Have a look at the page on AsyncTasks above for a complete overview. It's really simple to implement you just need to be aware of configuration changes if your AsyncTask has not completed.
You might also want to look into caching your bitmaps in memory to prevent them from being unnecessarily downloaded again.
public class DownloadItemDetails extends AsyncTask<String, Void, ArrayList<Bitmap>{
protected ArrayList<Bitmap> doInBackground(String...params){
//Download each Bitmap here, and add them to a list to be displayed later
}
protected void onPostExecute(ArrayList<Bitmap> results){
//This method executes on the main thread, so after your downloads finish
//You can set your imageviews in your Details Page here
}
}
According to your curiosity to make user happy, I think you have to use Android Universal Image Loader library from Github. This library provide you asynchronous image loading, caching and displaying.
You can also use ShutterBug - Remote Image Loader from Github for same. It also fetch remote images and cache them. It is particularly suited for displaying remote images in lists, grids, and maps.

Android application being too laggy

My Android application is running very slow and lagging much. I have PHP API on my server and my application requests data through HTTP.
Though, the problem is that sometimes I should wait for few seconds before I can see the result. I have all calculations done in the main thread in onCreate (parsing XML, adding controls) and downloading data from HTTP server in AsyncTask.
How to optimize my program to make it faster? I want it to load activity first and only then, in background, download and parse data. How is it possible? Sorry for newbieship.
what did you mean by lagging ? Will you elaborate more on the issue.
One suggestion that remove parsing XML from OnCreate and move it to AsysnTask. The reason for this is as you are doing time consuming operation in UI Main thread which will impact the activity to be shown.
Create thread to perform HTTP related operations and parse the response on the same thread and while doing the parsing operation show dialog.
Dismiss the dialog when parsing got completed and then show the activity which you want to display.
In the doInBackground() method in AsyncTask add the data parsing and create the data objects then in onPostExecute update the ui elements.
The reason to do something like that is for the application to be responsive in all this time and just make small jobs on the ui thread so as not to freeze.
You can instatiate your views to a default state an add a progress somewhere on top to indicate that the activity is currently loading. For example you can create an empty ListView or a Button that cannot be selected and when the parsing is done then you should set the adapter to the list and make the button back to selectable again.
All this things can be implemented according to what you want the user to be able to do in the time of his waiting.

Android - How do i execute code *after* a process is loaded

Basically when a menu item is pressed in my app it takes the user to a new screen.
But now i want to get a little bit of data online(a time/date) that will display somewhere on the page once it gets the data.
At the moment the new screen doesnt load until the app gets the data which leaves a couple of second pause instead of loading the screen instantly and then showing the downloaded data when it finally has it.
Ive tried using the onStart method since that gets called after onCreate so i thought the page would be created and displayed and then onStart gets called but that doesnt happen, theres still lag between it loading because its getting the data online and then displays the page with it already loaded.
How can i sort this?
Thanks
For any kind of loading operation you should consider using the AsyncTask in combination with the ProgressDialog in order to inform your user that you're performing something in the background.
Remember that you can only interact with the UIThread in the onPreExecute() and onPostExecute() method of your AsyncTask.
So in your case you'll probably want to fire the AsyncTask in the onCreate() method of your Activity, showing the ProgressDialog in the onPreExecute() method and then showing your information and hiding your ProgressDialog in the onPostExecute.
It's pretty much impossible to eliminate this lag you're talking about. So you better just handle it appropriately.
Hope this helps answers your question.

One loader for all activities

Im building an Android application. It parses a feed which is stored in a DB. Each activity of the app is able to get this data from the DB.
Every activity can also call the Service, and make it refresh the data. When this is done I would like to display a loader. While the Service is downloading, the user is still free to navigate between activities. My question is; how can I display a loader that runs in all activities the user navigates to?
Thanks a lot :)
Here's what I would try to do since it seems to be an uncommon task to me:
I would try to setup a translucent PopupWindow which contains a progress indicator. I would trigger / dismiss this popup to be displayed / or not whenever there's a need to indicate the loading progress...
You could try something like:
Place a "global loader view" in all your activities.
When you start/return to an activity, fire an AsyncTask which will be used to handle updates of the global loader
Override onPreExecute() to prepare the global loader (e.g. setting its state to the current level if the service has been downloading for a while)
Override onProgressUpdate() and use it to update the state of the global loader by asking the Service for the current state (load percentage or something)
In doInBackground you implement:
while( service.isLoading() ) {
publishProgress( service.getLoadPercentage() )
// Maybe add a Thread.sleep() here.
}
Override onPostExecute to set the visibility of the global loader to View.GONE.
You might need to implement a way of killing the AsyncTask if the user switches away from the activity when the loading is not finished yet.

Categories

Resources