Search code examples
javaandroidlockscreenonpause

Android onPause() called after onStop()


I'm trying to display an Activity over lockscreen. The Activity is displayed for a few moments and then crashes. This happens only on a particular device (Sony Xperia Z2), the app is running completely fine on a variety of other devices.

According to log, onPause() is called after onStop():

10-28 16:14:03.079: D/AlarmAlert(29630): onCreate CALLED
10-28 16:14:03.099: D/AlarmAlert(29630): onCreate DONE
10-28 16:14:03.099: D/AlarmAlert(29630): onResume
10-28 16:14:03.109: D/AlarmAlert(29630): onPause
10-28 16:14:03.119: D/QSEECOMAPI:(322): Loaded image: APP id = 4
10-28 16:14:03.139: W/SurfaceFlinger(320): couldn't log to binary event log: overflow.
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: KEY_TYPE=106, KEY_STATUS=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: data=marlin,1, status=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: KEY_TYPE=109, KEY_STATUS=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: data=playready,1, status=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: KEY_TYPE=110, KEY_STATUS=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: data=dtla,1, status=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: KEY_TYPE=111, KEY_STATUS=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: data=marlin_import,1, status=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: KEY_TYPE=112, KEY_STATUS=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: data=ckb,1, status=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: KEY_TYPE=113, KEY_STATUS=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: data=widevine,1, status=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: KEY_TYPE=114, KEY_STATUS=1
10-28 16:14:03.149: E/suntrolkac(396): props_GetStatusForAllKeys :: data=dcp,1, status=1
10-28 16:14:03.149: I/ActivityManager(29630): Timeline: Activity_launch_request id:com.mypackage.alarm time:34436028
10-28 16:14:03.149: I/ActivityManager(989): START u0 {flg=0x10040000 cmp=com.mypackage.alarm/.AlarmAlert} from pid 29630
10-28 16:14:03.179: E/AudioSink(308): received unknown event type: 1 inside CallbackWrapper !
10-28 16:14:03.199: E/AudioSink(308): received unknown event type: 1 inside CallbackWrapper !
10-28 16:14:03.239: D/AlarmAlert(29630): onStop
10-28 16:14:03.279: I/ActivityManager(29630): Timeline: Activity_idle id: android.os.BinderProxy@4487f768 time:34436159
10-28 16:14:03.359: D/dalvikvm(23275): GC_CONCURRENT freed 1210K, 9% free 53975K/59108K, paused 7ms+18ms, total 92ms
10-28 16:14:03.359: D/dalvikvm(23275): WAIT_FOR_CONCURRENT_GC blocked 44ms
10-28 16:14:03.359: W/eqb(23275): Finalizing LiveList without having stopped producer: eom{type=SAPI_LL_TYPE_PREFETCH_DETAILED_ITEMS, priority=DEFAULT, isPaused=false, #elements=0}
10-28 16:14:03.359: W/eqb(23275): Finalizing LiveList without having stopped producer: eom{type=SAPI_LL_TYPE_PREFETCH_DETAILED_ITEMS, priority=DEFAULT, isPaused=false, #elements=0}
10-28 16:14:03.359: D/dalvikvm(23275): WAIT_FOR_CONCURRENT_GC blocked 46ms
10-28 16:14:03.369: D/dalvikvm(23275): WAIT_FOR_CONCURRENT_GC blocked 55ms
10-28 16:14:03.509: W/cumulus(23275): iup: Disposing queries [66d230c9722515cd, 315aba24fcc62b40, 58661e75e6173090, 158eab12335e048f, 21682fbd5a0aee5, 8faf972cbd2d5fa, 34797718aabd5c44, 1907fb5f55c1a2b9, 3b40d400e43e2c7]
10-28 16:14:03.509: W/cumulus(23275): hqt: Channel closed.
10-28 16:14:03.539: D/dalvikvm(989): GC_FOR_ALLOC freed 2434K, 14% free 86915K/100260K, paused 183ms, total 183ms
10-28 16:14:03.549: D/AlarmAlert(29630): onPause
10-28 16:14:03.549: D/QSEECOMAPI:(322): QSEECom_dealloc_memory 
10-28 16:14:03.549: D/QSEECOMAPI:(322): QSEECom_shutdown_app, app_id = 4
10-28 16:14:03.549: I/ActivityManager(989): Timeline: Activity_windows_visible id: ActivityRecord{4508a378 u0 com.mypackage.alarm/.AlarmAlert t176 f} time:34436428
10-28 16:14:03.549: E/ActivityThread(29630): Performing pause of activity that is not resumed: {com.mypackage.alarm/com.mypackage.alarm.AlarmAlert}
10-28 16:14:03.549: E/ActivityThread(29630): java.lang.RuntimeException: Performing pause of activity that is not resumed: {com.mypackage.alarm/com.mypackage.alarm.AlarmAlert}
10-28 16:14:03.549: E/ActivityThread(29630):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3133)
10-28 16:14:03.549: E/ActivityThread(29630):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3121)
10-28 16:14:03.549: E/ActivityThread(29630):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3099)
10-28 16:14:03.549: E/ActivityThread(29630):    at android.app.ActivityThread.access$1000(ActivityThread.java:144)
10-28 16:14:03.549: E/ActivityThread(29630):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1263)
10-28 16:14:03.549: E/ActivityThread(29630):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 16:14:03.549: E/ActivityThread(29630):    at android.os.Looper.loop(Looper.java:212)
10-28 16:14:03.549: E/ActivityThread(29630):    at android.app.ActivityThread.main(ActivityThread.java:5151)
10-28 16:14:03.549: E/ActivityThread(29630):    at java.lang.reflect.Method.invokeNative(Native Method)
10-28 16:14:03.549: E/ActivityThread(29630):    at java.lang.reflect.Method.invoke(Method.java:515)
10-28 16:14:03.549: E/ActivityThread(29630):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-28 16:14:03.549: E/ActivityThread(29630):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
10-28 16:14:03.549: E/ActivityThread(29630):    at dalvik.system.NativeStart.main(Native Method)
10-28 16:14:03.559: I/ActivityManager(989): Activity reported stop, but no longer stopping: ActivityRecord{4508a378 u0 com.mypackage.alarm/.AlarmAlert t176 f}
10-28 16:14:03.559: W/ActivityManager(989): Activity ActivityRecord{4508a378 u0 com.mypackage.alarm/.AlarmAlert t176 f} being finished, but not in LRU list
10-28 16:14:03.569: I/PowerManagerService(989): Waking up from sleep...
10-28 16:14:03.579: D/SurfaceFlinger(320): Screen acquired, type=0 flinger=0xb87e5130
10-28 16:14:03.579: D/qdhwcomposer(320): hwc_blank: Unblanking display: 0
10-28 16:14:03.589: D/PhoneStatusBar(1284): disable: < EXPAND icons alerts TICKER system_info back* home* RECENT clock* SEARCH* >
10-28 16:14:03.589: V/KeyguardServiceDelegate(989): onScreenTurnedOn(showListener = com.android.internal.policy.impl.PhoneWindowManager$25@45a6a158)
10-28 16:14:03.859: I/qdhwcomposer(320): handle_blank_event: dpy:0 panel power state: 1
10-28 16:14:03.869: D/qdhwcomposer(320): hwc_blank: Done unblanking display: 0
10-28 16:14:03.919: I/Choreographer(1614): Skipped 39 frames!  The application may be doing too much work on its main thread.
10-28 16:14:03.929: E/XperiaLockscreen(1284): Setting cancellation runnable when already set
10-28 16:14:03.939: D/FULLockscreen(1284): Binding to Face Unlock service for user=0

And here is my Activity:

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.HapticFeedbackConstants;
import android.view.View;
import android.view.WindowManager;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;


public class AlarmAlert extends Activity {

    ScheduledFuture<?> autoSnooze;
    SharedPreferences prefs;
    boolean isResumed = false;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.d("AlarmAlert", "onCreate CALLED");
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                             WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
                             WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|
                             WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON|
                             WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        setContentView(R.layout.activity_alarm_alert);
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        autoSnooze = scheduler.schedule(new Runnable() {
            @Override
            public void run() {
                snoozeAndFinish();
            }
        }, 60, TimeUnit.SECONDS);
        ((ResizeButton) findViewById(R.id.btn_snooze)).setCompoundDrawablesWithIntrinsicBounds(AlarmHome.isSmall(this) ? R.drawable.ic_clock_scalable : R.drawable.ic_clock, 0, 0, 0);
        ((ResizeButton) findViewById(R.id.btn_dismiss)).setCompoundDrawablesWithIntrinsicBounds(AlarmHome.isSmall(this) ? R.drawable.ic_stop_scalable : R.drawable.ic_stop, 0, 0, 0);
        Log.d("AlarmAlert", "onCreate DONE");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onBackPressed(){
        Log.d("AlarmAlert", "onBackPressed");
        snoozeAndFinish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isResumed = true;
        Log.d("AlarmAlert", "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        isResumed = false;
        Log.d("AlarmAlert", "onPause");
    }

    @Override
    protected void onStop(){
        super.onStop();
        if (this.isFinishing()) {
            sendBroadcast(new Intent(Alarm.ALARM_ALERT_DISMISS));
        }
        Log.d("AlarmAlert", "onStop");
    }

    private void dismiss(){
        Log.d("AlarmAlert", "dismiss");
        autoSnooze.cancel(false);
        if (!prefs.getBoolean("alarmRepeat", false))
            Alarm.cancel(getApplicationContext());
        sendBroadcast(new Intent(Alarm.ALARM_ALERT_DISMISS));
        if (isResumed)
            finish();
    }

    public void dismiss(View view){
        view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        dismiss();
    }

    private void snooze(){
        Log.d("AlarmAlert", "snooze");
        sendBroadcast(new Intent(Alarm.ALARM_ALERT_DISMISS));
        autoSnooze.cancel(false);
        Alarm.set(this, System.currentTimeMillis() + 5*60*1000);
    }

    private void snoozeAndFinish(){
        snooze();
        if (isResumed)
            finish();
    }

    public void snooze(View view){
        view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        snoozeAndFinish();
    }
}

I am hopeless, does anyone know why is this happening?


Solution

  • I've killed the unprocessed thread execution in onStop(), after that first execution of the app was fine. But other time the activity was created, it kept crashing with the same error.

    This activity is started by a receiver, where i call Alarm.startRinging(Context) and some other time, when user leaves the activity i call Alarm.stopRinging(). In the first method, I stored a reference of a Ringtone object in a static variable. In the second method I've executed ringtone.stop().

    I've created a service with non-static Ringtone variable for playing sounds and it now works.