1

I'm planning to create an app that basically asks a lot of questions (for the purpose of study). I haven't started it yet, I'm still planning how I will carry it out.

I'm thinking of creating a class called Question and then storing multiple objects of that class in different arrays. I could end up having thousands of Question objects. I would do this in my onCreate() method, so as to have all the questions ready by the time the app starts.

Would doing so much in the onCreate() method slow down/cripple my program? What makes this worse is that if the user exits the app and then reopens it, the questions would be added again.

Is there a better way of having large amount of objects in my app? As someone fairly new to android development and programming in general, I appreciate any help.

  • Why do you need to have thousands of Questions in memory at once? Certainly the meta data for these questions will reside somewhere else. Probably on a server or a file on the local file system. Also, how do you plan on the user accessing these Questions? I assume one at a time. If so, why not just create a single Question just when you need it? To avoid asking the same Question twice, you could have some state to keep track of already asked Questions which could be as simple as keeping track of an index. – Michael Krause Jul 15 '18 at 04:07
  • Well like I said I'm new to android development so I'm not sure if I'm using the best approach. Should I look into using meta data? I want my app to be more interactive than just answering one question at time. Users will be able to choose what specific topic of questions they want, or they may want to be asked questions that they have gotten wrong a couple of times. This is why I felt I need Question objects. To be able to classify questions and update their status. – TheCovenant Jul 15 '18 at 04:17
  • OK - so you'll be showing Questions in a list. How will you be creating the Question objects? There'll be a String that has the actual question you show the user, and it sounds like Questions will have categories. There are two bits of meta data that will have to be stored somewhere in order to create the Question objects, right? Where will you get this data to begin with? – Michael Krause Jul 15 '18 at 04:22
  • I will be creating all of these questions by myself. And be sorting them into categories by myself. It's going to be a lot of typing and manual labor. As of right now the questions do not exist in a data file or anything, – TheCovenant Jul 15 '18 at 04:36

1 Answers1

0

It would be better to do the loading on a background thread (using an AsyncTask or other threading library) and to display some sort of loading progress bar to the user while it loads. In the onPostExecute method of your AsyncTask (or similar) then you can turn off the progress bar and turn on the relevant UI elements.

If you want the questions to persist between different activities, you could store the data in a singleton so it persists during the app session at least.

Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • Background thread, yes. AsyncTask, no. There are so many documented problems with AsyncTask that IMHO, they should just never be used. There are much better solutions, including, for instance, RxJava. – GreyBeardedGeek Jul 15 '18 at 03:47
  • Good point (some more reading on that here https://stackoverflow.com/questions/39214073/rxjava-instead-of-asynctask). Which I'd recommend here though depends how simple the application/task is for a new developer. Have a link for the AsyncTask problems you were referring to that might be applicable here? – Tyler V Jul 15 '18 at 03:54
  • The basic problem is that AsyncTask holds a reference to the Activity, but is not aware of the Activity's lifecycle. This easily leads to hard-to-find crashes. Here is one article explaining it:. https://blog.danlew.net/2014/06/21/the-hidden-pitfalls-of-asynctask/ – GreyBeardedGeek Jul 15 '18 at 12:22
  • Well that depends how you make the AsyncTask, it *is* easy to leak a context, but it doesn't *have* to hold a reference to an Activity... There is also the issue that by default they all run on the same thread and will block each other... – Tyler V Jul 15 '18 at 13:34