MW.
MW.

Reputation: 12630

Open new Android activity in background

Summary
Can I start a new activity from a background service when its application is in the background, without bringing the application to the front?

Background
Suppose I'm developing MyApp for Android. This app handles very sensitive information, so we need to lock the app when the user has been inactive for a little while.

MyApp has a service, MyService. Different user interactions with the app resets an inactivity timer in MyService. When the inactivity timer expires, the service starts a new activity, LockActivity, which acts as a screen lock for MyApp. The user has to reauthenticate herself to get past the LockActivity and resume working with the app.

This all works, with one problem: when the LockActivity is started, it brings the app to the front. Since the user may be doing something else (browsing Facebook or whatever), she will be annoyed, and rightly so.

The code I'm using for starting the activity from the background is:

Activity topActivity = magicallyFindMyTopActivity(); // This part is not important; it works though
Intent intent = new Intent(this, LockActivity.class);
topActivity.startActivity(intent);

Do you know any way to avoid this?

Upvotes: 0

Views: 434

Answers (3)

Arun Kumar
Arun Kumar

Reputation: 194

Don't blindly start lock activity when inactivity timer expires, just set some variable and when your app resumes or starts check variable state and show lock screen first.

Upvotes: 0

Sid
Sid

Reputation: 14916

An Activity is almost every time something that shows up to the user, so the user can interact with it.

I think what best fits what you are trying to archive is to use OnResume event and check for a field which tells if the app is secured.

Something like this:

onResume(..){
  if(isSecured){
     _secureMyApp();
  }    
}

Have a look at this: enter image description here

Upvotes: 1

priyank
priyank

Reputation: 2691

Check the security thing in background service at some interval, now have a flag

boolean secure = true;

When the time expires update the flag secure = false;

In your main activity check the flag every time if its false ask the user to authenticate. (Don't create any new activity)

Upvotes: 1

Related Questions