Notify current Activity which background function has finished running Android

Asked

Viewed 178 times

0


I have a class that looks for a list of clients in the background with a AsyncTask. She can be called any Activity, and returns nothing, just looks for the list of customers that comes in json and saves that list as string in a SharedPreferences.
Initially, I’m sending to search the list on my login screen, so when I bring the user data from the database, it already sends to search the client list (to reduce the data search wait), but I needed when this class saved the list on SharedPreferences my Activity which opens after the login is notified so I fill a RecyclerView with her. I thought of Implementing a Interface in AsyncTask but she returns to the Activity who called her.
Do you have any method to notify Activity even if the function was not started by it?

Note: If you do not understand the question, comment before negative that I will be happy to clarify your doubts.

  • Yes it is possible, but this approach may fail if the list is saved before the activation of the activty. In that case the Activity does not receive the notification.

  • Is there any other way?

  • The normal is the data to be read by the Activity that uses them or be passed on to her.

  • I’ve done it but sometimes it takes too long and I need the search to be quick.

  • If you want to try, even though you know you can fail, look at this reply.

  • I’ll take a look.

Show 1 more comment

1 answer

1


Good afternoon,

It has how to do what you want in various ways, for a simpler solution I use a BroadCastReceiver but if you’re going to use it in multiple classes, you might want to create an interface for this.

Here’s an example from Braodcast:

In the Activity

BroadCastReceiver myReceiver;
 protected void onCreate(Bundle savedInstanceState) {

 IntentFilter filter = new IntentFilter();

            filter.addAction("lista.atualizada");

            myReceiver = new BroadcastReceiver() {  //  < ------ Declare o Bradcast como global

                @Override
                public void onReceive(Context context, Intent intent) {
                   // Atualize o que quiser, foi atualizado

                }
            };
            registerReceiver(myReceiver, filter);

}


 @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver); // <---- não esqueça de destruir o receiver junto com a activity
    }

So in your task:

Intent atualizouLista = new Intent();

atualizouLista.setAction("lista.atualizada"); // < ---- aqui é o nome da sua "ação" a mesma que você registrou no receiver
mContext.sendBroadcast(atualizouLista); // < --- repare que estou passando mContext, na sua Task você precisa manter uma referência do seu contexto.
  • has a problem if Activity has not yet been opened?

  • The receiver is registered in the onCreate method, so it is linked to the Activity Life Cicle (onCreate,onStart,onResume,etc.), if you are going to start a service in an Activity and receive the result in another, you will need to implement a "Bus"which would be a Thread that would run without being linked to lifecicle. It’s kind of boring to do this on the nail, so I recommend using some Lib, I usually use Robospice (which I even integrate with Retrofit, Gson and Ormlite easily), but if I’m not mistaken Otto does something similar

  • @Brunoromualdo Whatever notification system is used (including an "Eventbus"), if the notification is launched before you have registered to receive it, it will not be received.

  • @ramaral Yes, you will lose the notification, unless you use some cache, Robospice keeps the notification in the cache, so when registering Systems you can recover it.

  • Look at this infographic to better understand, you can run the service and receive it in an Activity that has not started yet. https://raw.githubusercontent.com/stephanenicolas/robospice/master/gfx/RoboSpice-InfoGraphics.png

  • I was just looking at that, if you take a good look it notifies a new (same/recreational) Activity and not a different Activity.

  • @negative ramaral, check that in the infographic itself shows the notification in the "back", I use it as follows : I start the search service, the user keeps moving and my activitys have a service Manager, then, regardless of which Activity the notification arrives from, the service runs in parallel and the Activity takes the cache or request (depending on some parameters)

  • I believe you’re right, I can’t complain because I’ve never used Robospice.

  • Thanks, I’ll take a look.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.