Load data before or after creating activity? - android

I know this is a general question, but I don't know where to ask this. I was wondering what is a good design practice: to load data before creating an activity; or to create the activity, load the data and populate the UI.
In my opinion, fetching the data before isn't a good idea since you have to pass the information in a bundle to the next activity. Furthermore, you are handling information that is not relevant to the current activity. The only reason I see to do this is if you have a LOT of data to load and you don't want to stop the user from interacting with the app the time being.
However, I've seen a lot of people who load the information in an AsyncTask before launching the activity, even if it's a few bytes. Why? Is this the expected way to do it?

You can't give 1 perfect answer for this question as there is many different ways of doing this.
If you have something that is similar to a news app you'll be able to load content minus images while browsing a list and if the user goes to detail you could load the image on the fly and just display the text. But thats just one way of doing it. Some might prefer loading the image before displaying a detail screen.
My advice will be to think about your users and their work flow and you won't fail completely. And take this question on a case to case basis.

Related

How to load data from database for viewpager

In simpler terms, I have a viewpager2 which is supposed to display words and their meanings and users can be able to search and go a specific word just like a dictionary application. The problem is that I have a very huge database of a million words and loading it at once that's forever and slows the app down.
What's the best way to implement this?
My idea was to load five words at a time and load another set if the last word is reached but this seems impractical if the user search for another word. Is there a better method?
Before of all load it in another thread, and also try to load by limiting result

Lazy Loading Image concept definition and Implementation

OK so there are 2 concepts for Lazy Loading from all I have read.
Load images in a background thread.
Only display the image when the user is not interacting with the screen. (i.e. display it when the user have stopped scrolling)
My question would be, which one of those would be the right one.
For implementation, I'm using Universal Image Loader. Is it possible to implement concept #2 with this library natively, or should I add some extra code?
First off, you should know there isn't "right" or "wrong" one when it comes to user experience. It all depends on your way of thinking. However, based on Android guidelines, you should never perform heavy tasks on the main tread as it might affect user experience. It means you should always try to load images on a background thread.
But the question is whether you should keep loading and unloading the images as the user scrolls or you should only display the image once the user stops scrolling. I "personally" believe that the answer depends on the situation. For example imagine a your facebook friends list where there is names tags beside the images. In this case, you might not need to load all pictures as the user is able to search the list, scroll up and down and pick the right friend based on the name. However, if the user is expected to pick and select merely on the image. Then for sure the answer is to display the image once it is available.

best way to load data from a webservice into a listview

I'm making an adapter that it underline data loads from a web service and each time the user scrolls down it loads more data and adds it to the ArrayList behind that adapter. The data contains image URLs which I then lazy load in my getView method.
Is there a better solution for that?
And how do I properly cache those images?
And can I retain the data of the adapter when the user rotates the device? So no need to load them again from the beginning?
I use fragment to display that list?
I'll try to answer all three questions one by one.
is there a beter solution for that? (other then lazy-loading)
That depends. The nice part about lazy-loading is, that you don't need to have all data right at the start when you display the component. The problem often is, how to get new data, as it is needed.
So, when loading the new data from the network, it is really a question of how big the chunks of loaded data are. I would not load one new entry after the next one, but load e.g. 20 new ones, when the end of the list has been reached.
Of course this is just a random idea for a number, you'll need to find the perfect point between speed and usability for your case.
So when you are loading chunks of data from your web-service, which are delivered fast enough, it really is a cool concept. But if you're just reading one entry after another (and therefore, the user almost always waits for new data), this is really just annoying.
And how to proper cache the images?
There are multiple possible solutions available, some of those are:
Using the system-cache, like the browser (seems to be the
finest).
Implementing your own system which uses device-storage (e.g. the
SD-Card or the internal application storage (the first one is
preferred)).
You might also like the contents of this question (and it's accepted answer), giving you some general advises on memory-management.
can i retain the data of the adapter when the user rotates the device?
Old, deprecated anser:
Yes, you can use the "instance state" of the Activity to do this.
Basically, the onSaveInstanceState()-method from the Activity
allows you to populate a given Bundle with state-information, which
is then passed to the onCreate()-method.
The data you would save in the Bundle would be (for example) the
current position of the list or maybe even the list itself (depending
on the size).
Yes, use the Loaders API. It is also available in the compatibility library.

Android: Best way to Display Images on a screen

This could potentially be a duplicate but I after spending several hours reading about ImageView, ImageSwitcher, Gallery Views, Lazy Loads, and more I decided I should just ask.
I am working on an app and in one of the screens there may be an image or multiple images associate with what I am talking about. The other data for this intent will be pulled from a local database. In the database it will also have a list of URLs of images that are associate with each thing that I am discussing. So I would like to know the best way to display a thumbnail of these pictures (if there are any) on the screen. Then if these pictures are clicked on it can open up the bigger version of these pictures. I don't necessarily want to download these pictures since I don't want to save them locally for the app. Plus I don't want to make the page delay in loading while it waits to download the pictures.
What is the best way to handle this scenario?
I have to caution you, that with newer versions of Android, applications can't make connections to the internet that build the UI directly on the UI thread. Instead, it must be accomplished in a background thread such as ASyncTask. So if you're going to go with making ImageViews without src in your XML, that's fine, but you can't just have the ImageViews go download their images via URL in the onCreate of your main activity any longer. It has to be done in the background, else you'll get an error.
It makes the process fairly more complex, to be honest, but it does improve application responsiveness.
For now, since images aren't the main focus of my app, I decided to just embed a webview in my page and then display the image in some custom html ( I had a list of URLs to the images online). I also calculate the width of the screen and then I adjust how wide the images are going to be in my html.
This was the easiest since the webview takes care of the "threaded" part and I don't have to worry about how to handle/save/and display the images.
It would have been helpful to see a best practices guide when it comes to android and handeling images.

What's the best way to display a bunch of images from a web server in Android?

I need to load several thumbnails (the images on the server are full sized images, so I need to resize them in the app), and display them in a way that allows for the user to click on one (or more) and have some associated id be given back to the app for use in a larger context.
so, in pseudo code...
ImageObjArray ioa = loadImagesFromServer("http://server/listOfImages.php");// this returns
for(ImageObj io : ioa){
drawThumbnail(io); //io contains both a jpg and a reference id
}
//later when a user clicks on a thumbnail
clickHandler(){
passIdToBundle(this.refId);
}
So, I've never played with a webview before but I suspect it might be the best approach here, though I don't know how easy it is to send info back out of a webview to the larger app when the user clicks on a thumbnail. Or is there a more elegant approach?
All advice welcome.
Using a WebView would be the quickest way to implement this. Otherwise, you have to fetch each image and write them to the device in order to display them in a native Android widget. There are several ways to approach this, like: http://www.dreamincode.net/code/snippet4724.htm
You can use WebView.addJavascriptInterface to communicate with native code. A good example can be found in the WebViewDemo from the apps-for-android project on Google Code.
You might want to consider a framework like PhoneGap to help with the Java to JavaScript interface.

Categories

Resources