Karesh A
Karesh A

Reputation: 1751

Retrofit response after activity finish

After user logged in successfully, there will be another api call to get the user information. But before the response I will close the activity and start the home activity.

public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
    //save token

    // call REST service to get user info and update the user records on DB.
    // update the notification token info

    //go to home page
    Intent intent = new Intent(SigninActivity.this, HomeActivity.class);
    startActivity(intent);
    finish();
}

How can I make the call REST calls outside the activity. So App can handle the onReponse even after I close the Activity.

In General How can we call the API to do the background activity (eg: update database.) without any interaction . (response doesn't depend on activity)

Upvotes: 1

Views: 1518

Answers (3)

capt.swag
capt.swag

Reputation: 10661

There are two ways of doing this

  1. Calling the user information API from the home activity (This is what I prefer).
  2. While logging in, start a Service which makes the user information API call. Since Services are independent of UI and can run in the background starting the home activity and stopping the current one doesn't have any effect on the service. Once you get the response in the Service, create a broadcast(Or use EventBus) to send the response to home activity.

Upvotes: 0

AnthonyK
AnthonyK

Reputation: 473

You could run a service(async) in the background minimize the first app and onResponse kill the first app and start the 2nd. And just access the data from the first.

Upvotes: 0

Nabin Bhandari
Nabin Bhandari

Reputation: 16429

The easiest solution would be to use static method. You can call those methods from anywhere.

If you want to update UI elements inside that method, pass the UI elements as parameters to the static method.

Upvotes: 1

Related Questions