Posts

Showing posts from 2013

Activity life cycle

From http://androidcookbook.com/Recipe.seam?recipeId=2688 onSaveInstanceState(Bundle) and onPause() Use onPause to save data that should be preserved long term, use onSaveInstanceState for transient states. For example in a game the current score could be saved in onSaveInstanceState and only saved in onPause if it was good enough for the High Score table, in which cause the High Score table (in SharedPreferences, a database table, or a file) is updated. onSaveInstanceState\onRestoreInstanceState and the Android Lifecycle The SDK documentation says that  onSaveInstanceState(Bundle)  may run just before or just after  onPause() . As for onRestoreInstanceState(Bundle)  that occurs after  onStart()  and before  onResume(). onSaveInstanceState() is not called when the user navigates to another activity. So if the user presses the back button, this callback won't be called. But if the Android home button is pressed this callback is call...

Multi Threading in Android

It is good practice to not do computation intensive work on the UI Thread. In fact starting from HoneyComb, you get a NetworkOnMainThread exception if you try and do any network processing on the main thread. So we should do background work in a new thread. One thing we need to consider is that only the UI thread can update the UI, so when our background thread wants to update the UI, we need a means to communicate between threads. Actually even your background thread can update the UI, but this is really dangerous and your app can crash! Android provides a lot of multi threading constructs for us. AsyncTask is by far the easiest way of multithreading in Android. When you create a new AsyncTask object the framework creates a new thread for you and provides methods to communicate with the UI. No threads, no handlers and no Executors (Because it uses these classes internally)! So why not use AsyncTask all the time? Well, because these are suited for background work that typically l...

Env vars on Ubuntu

To set them, export VAR=1 To check them, printenv To unset, unset VAR. On Ubuntu, you can set vim ~/.pam_environment and then write: JAVA_HOME=/usr/local/bin/jdk1.6.0_30 PATH DEFAULT=${PATH}:${JAVA_HOME}/bin Or, you can go to ~/ .bashrc and write: export JAVA_HOME="/usr/local/bin/jdk1.6.0_30" export PATH=$PATH:$JAVA_HOME/bin

Vim pasting

Vim keeps messing up your pastes. Type :set paste Pasting it now works. Once done, you can type :set nopaste Thanks to http://www.protocolostomy.com/2008/04/04/vim-messing-up-indentation-on-your-pasted-code/

High Res timer on Win using C++

Took this from http://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter #include double PCFreq = 0.0 ; __int64 CounterStart = 0 ; void StartCounter () { LARGE_INTEGER li ; if (! QueryPerformanceFrequency (& li )) cout << "QueryPerformanceFrequency failed!\n" ; PCFreq = double ( li . QuadPart )/ 1000.0 ; QueryPerformanceCounter (& li ); CounterStart = li . QuadPart ; } double GetCounter () { LARGE_INTEGER li ; QueryPerformanceCounter (& li ); return double ( li . QuadPart - CounterStart )/ PCFreq ; } int main () { StartCounter (); Sleep ( 1000 ); cout << GetCounter () << "\n" ; return 0 ; }