JohnnyLambada
JohnnyLambada

Reputation: 12826

Android Service - Can it pop an Activity?

I have three separate applications A, B and C. All of them use service S. When A, B or C connect to service S and service S must start, it must gather credentials from the user using form F.

I want the user experience to go like this:

  1. From the Android main menu, the user taps one of the applications; A, B or C.
  2. Form F appears because the service has not been started.
  3. The user fills in form F and taps OK.
  4. Validation occurs.
  5. If validation of credentials is successful, then the appropriate Activity in the selected app (A, B or C) appears.
  6. After a successful validation, any subsequent applications that start do not need to authenticate, they'll just start with an Activity in that app.
  7. If the validation is unsuccessful and the user presses the back button, the main menu appears again.

Given all of this, what I believe I want is for the Service to start the credentials form when necessary -- can a Service do this? If not, what would be an alternative way to implement this?

Upvotes: 0

Views: 1307

Answers (2)

Nikhil_Katre
Nikhil_Katre

Reputation: 554

A Service should never start an Activity directly. This is clearly stated in the documentation on Notifications. You can view it at this link (please refer to the second paragraph).

The scenario that you described can be handled in an alternate way:

  1. Add a method to the service isValidated() which returns true if the user has already been validated.
  2. Activity A, B or C should use isValidated() to first check whether the user is validated or not. If not the Activity should show the from F.

Upvotes: 1

synic
synic

Reputation: 26678

A Service is a Context, and a Context can start an Activity:
https://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent)

Upvotes: 2

Related Questions