Search code examples
androidandroid-asynctaskonresumeonpause

Android Why OS issues onPause()


My Activity creates an AsyncTask that loads data from a file and updates the UI on onProgressUpdate(), classic book AsyncTask.
But Android is putting the Activity onPause() right after the AsyncTask is launched.

I am trying to understand why (and how to prevent it)

This causes all AsyncTask UI updates to stop; then, when the AsyncTask finishes, the Activity does not resume by itself, I have to touch the screen or press some Action bar button to get it to onResume()

EDIT:
This is the logcat, (using aLogcat app to get it)

< To be updated with new complete log >

Temporary (first onPause() after file chooser):

06-12 22:08:48.744 11302 11302 D Viewer: + optionsItemSelected(itemID:2131296267)
06-12 22:08:48.754 11302 11302 D Viewer: + showFileChooser(path:/mnt/sdcard/Android/data/com.delphi.app/files, requestCode:0)
06-12 22:08:48.754 11302 11302 D Viewer: + onResume()
06-12 22:08:48.754 11302 11302 D Viewer: + onSaveInstanceState(outState:Bundle[{}])
06-12 22:08:48.754 11302 11302 D Viewer: + onPause()
06-12 22:08:48.754   189   443 I ActivityManager: START {act=android.intent.action.CHOOSER cmp=android/com.android.internal.app.ChooserActivity (has extras)} from pid 11302
06-12 22:08:48.764   189   489 D WindowManager: adjustConfigurationLw, config:{1.0 0mcc0mnc (no locale) layoutdir=0 sw800dp w800dp h1232dp xlrg port ?uimode ?night finger -keyb/v/h -nav/v} mLidOpen:-1 mHasDockFeature:true mHasHallSensorFeature:true config.hardKeyboardHidden:2
06-12 22:08:48.764 11302 11304 D dalvikvm: GC_CONCURRENT freed 244K, 5% free 6748K/7047K, paused 2ms+3ms

Temporary (second onPause() , the probelmatic one):

06-12 22:09:00.324 11302 11350 I LoaderTask: Publishing message 13
06-12 22:09:00.324 11302 11350 I LoaderTask: Adding message 14
06-12 22:09:00.324 11302 11350 I LoaderTask: Publishing message 14
06-12 22:09:00.354 11302 11350 I LoaderTask: Adding message 15
06-12 22:09:00.354 11302 11302 D MessageTable: + onLayout(changed:false, l:0, t:0, r:800, b:20)
06-12 22:09:00.354 11302 11350 I LoaderTask: Publishing message 15
06-12 22:09:00.364 11302 11302 D MessageTable: + getColumns()
06-12 22:09:00.364 11302 11302 D MessageTable: + getRowCount()
06-12 22:09:00.364 11302 11302 D MessageTable: + getRowAt(index:-1)
06-12 22:09:00.364 11302 11302 D MessageRow: + getColumns()
06-12 22:09:00.364 11302 11302 D MessageTable: + setHeaderColumns(cols:[I@41125668)
06-12 22:09:00.364 11302 11302 D MessageRow: + setColumns(cols:[I@41125668)
06-12 22:09:00.364 11302 11302 D Viewer: + onSaveInstanceState(outState:Bundle[{}])
06-12 22:09:00.364 11302 11302 D Viewer: + onPause()
06-12 22:09:00.364 11302 11302 D LoaderTask: + getState()
06-12 22:09:00.364 11302 11302 D LoaderTask: + pause()
06-12 22:09:00.364   189   489 D WindowManager: adjustConfigurationLw, config:{1.0 0mcc0mnc (no locale) layoutdir=0 sw800dp w800dp h1232dp xlrg port ?uimode ?night finger -keyb/v/h -nav/v} mLidOpen:-1 mHasDockFeature:true mHasHallSensorFeature:true config.hardKeyboardHidden:2

App structure, added upon request:

MainActivity
    Launch Viewer Activity on Button click

Viewer Activity
    Show layout, has a MessageTable (TableLayout)
    Launch ShowFileChosser  to choose a Records file
    Execute LoaderTask with given file

LoaderTask
    Open given file
    doInBackground()    
        Read one record
        Add the record to an ArrayList
        if not paused Publish record
    onProgressUpdate()
        Show record in MessageTable

Solution

  • 06-12 18:21:09.394 9512 9512 D Viewer: + onStop()
    06-12 18:21:14.684 9512 9512 D Viewer: + onActivityResult(requestCode:0, resultCode:-1, data:Intent { dat=file:///mnt/sdcard/Download/Records.11111.MyLog.gzip })
    

    You've filtered the logcat so it's hard to see all of what is happening, but it looks like you are using an Intent to get something (a filename?) from another activity during this 5 second gap.

    You should expect an onPause()-onResume() cycle anytime you transfer to another activity.