简体   繁体   中英

Android activity opens twice when starting from lock screen

Hey guys last weeks I have tried myself with some android development and I'm currently stuck on the following error: Im running a timer with a service in background. When the time is up an activity shall open, even when the phone is locked. With the following code, while not locked everything is fine.. But when opening from locked screen it will always open up twice.. :/

I have added this in the onCreate to make it open from lock screen.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //allow window to be popped up while in lock screen
    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.activity_entry);

And I am opening the activity from the service via an intent.

    callEntryActivityIntent = new Intent(this, EntryActivity.class);
    callEntryActivityIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    pendingIntent = PendingIntent.getActivity(this, 0, callEntryActivityIntent, 0);

and when timer is finished -> startActivity(callEntryActivityIntent);

maybe someone has an idea. I'm really really new to android development, started this two weeks ago.

I am new too, but I guess **android activity cycle** has an answer:
+ The user opens an activity.
       - onCreated() is called
       - onStart() is called
       - onResume() is called  
+ The user LOCKS the device 
       - onPause() is called
       - onDestroy() is called
       - onCreate() is called
       - onStart() is called
       - onResume() is called 
       - onPause() is called   
+ The user UNLOCKS the device
       - onResume() is called
       - onDestroy() is called
       - onCreate() is called
       - onStart() is called
       - onResume() is called.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM