Android constructors

So after some time programming Android apps (1 uploaded to the market, you have 3k + active installations with a 4.7 rating), I started to wonder how I can make the app even more awesome. I realized that I really couldn't add any new and changing features in the world, so I started checking the performance and how to optimize pretty much everything, how to find the best CPU / memory utilization, etc.

Anyway, I found out that onCreate will work if the screen is rotated, which makes sense, but there are some (big) calculations that definitely don't need to be repeated after each rotation. One is iterating through a csv with 6500 rows having 4 columns, 2 of which always contains some data, 2 of which is not always. The 2nd data column will be used for the autocomplete adapter, the other 2 is optional for the function, but it still needs to be initialized. This currently works in an asynctask running at the end of onCreate and takes about 3 seconds on my HTC Desire S, which has a decent processor, so lower budget devices will have longer initialization times after each turn, which of course I don't want ... It won't crash the UI,but there won't be any autocomplete until the seconds are gone.

SO: to my question: can I do this in some separate method, like a constructor (like in standard java), or is it bad practice because of the special lifecycle of actions? I mean, I create my activity the way the "constructor" would work, and right after that my onCreate will run. In case of rotation, my "constructor" won't run again, but onCreate will. Stability will continue to be my nr1 goal. Or, is there a good way to do this? Something that was created that way that I don't remember? I really want to improve a lot on this issue and I would really appreciate some help on this, preferably from someone with experience with this, but any help is appreciated! :)

For example, if I want to make a new activity this way, I would do it something like this:

new MyActivity(some parameters);

      

so the constructor is executed, which ends with something like this:

startActivity(new Intent(context, MyActivity.class));

      

This way the constructor is triggered, my variables will be initialized (not connecting to any kind, etc.) and after that my activity can fire the onCreate variable at any time.

Excuse me if I'm wrong about the syntax, I just fasttyped it :)

+3


source to share


2 answers


You have to separate this logic from your activity. There are many ways to do this, but the end goal is for your csv parsing to be done in another class, and that class should provide information on whether the data has already been parsed. So, in onCreate, you call your class to get the data. If it already exists, you get your cached data right away. If this is the first time the method is called, or for some reason your cache has been cleared, you parse your csv file and do whatever calculations you need.



+1


source


you can take a look at onRetainNonConfigurationInstance which can return an object that you can access after re-creating your activity. will just return an object containing all the processed onCreate results and next time you check if getLastNonConfigurationInstance () is there - and don't recalculate everything



0


source







All Articles