Search code examples
javaandroidandroid-studioandroid-activity

Launching activity from the background, doesn't run the function in onResume()


In my application, when the app stops recording audio it launches another activity and runs the function automaticSync() in the onResume(), but when the device is locked it does not run the function from the background, is there a way to run a function in the second activity from the background when launched.

Recording Activity launching another activity

if (autoSyncSwitchSelected1) { // If user's preference is automatic syncing then set the 
                               // activity to syncRecording which will call the 
                               // automaticSync() method
                                    
   handleSelectedFiles("syncRecordings", 3); // Start activity RecordingsExplorer for result
                            
}

Recording Explorer

@Override
protected void onResume() {
    super.onResume();
    System.out.println("onResume");
    // get default shared preferences file:
    settings = PreferenceManager.getDefaultSharedPreferences(this); 
    autoSyncSwitchSelected = settings.getBoolean(Config.PREF_AUTO_SYNC, true);
    if(autoSyncSwitchSelected) {
        automaticSync();
    }
}

Solution

  • OnResume is the state in which the app interacts with the user. It's called when the app is in the foreground. As you said "when the device is locked it does not run the function from the background", yes this is correct. OnResume will only be called when the app is in the foreground. When your activity is no longer visible to the user or in the background, it has entered the OnStop State. Have a look on Activity-lifeCycle-Concept.