Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if my context is still valid?

Tags:

java

android

I'm working with a fairly common situation right now - download some data over the web, then update a view to display it. Clearly, I want to do the web download in the background, and then update the view on the main UI thread. Now looking at my code, I'm a little worried about my Activity and its UI elements being killed off before I update them. Here's the essence of what I have in mind:

Thread update = new Thread() {
    public void run() {
        final Data newData = requestData();                     
        if (newData != null) {
            post(new Runnable() {
                public void run() {
                    Toast.makeText(MyClass.this, "I'll do things here that depend on my context and views being valid", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
};
update.start();

It seems possible that while I'm downloading data, the activity may be destroyed. What happens then? Will my thread continue to execute? Will I end up trying to access dead objects?

Usually I do this by AsycTask, but the work seemed simple enough this time to just inline the threads-launching-threads stuff. Will I make things any better by using an AsyncTask instead?

like image 800
MaximumGoat Avatar asked Oct 21 '11 22:10

MaximumGoat


People also ask

When would you call getApplicationContext?

Use getApplicationContext() if you need something tied to a Context that itself will have global scope.

How many types of context are there in Android?

There are mainly two types of Context that are available in Android.

What is the difference between application context and context?

Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only. Activity Context: It is the activity and we are present in Activity.


2 Answers

If your Context is an Activity, you can check if it is finishing or has finished with the isFinishing() method:

if ( context instanceof Activity ) {
    Activity activity = (Activity)context;
    if ( activity.isFinishing() ) {
        return;
    }
}
Toast.makeText(context, "I'll do things here that depend on my context and views being valid", Toast.LENGTH_SHORT).show();
like image 74
JesperB Avatar answered Oct 02 '22 05:10

JesperB


What you really want to use is an AsyncTaskLoader. These are my new favorite classes in the Android API. I use them all the time and they were made to solve problems just like this. You won't have to worry about when to stop your download or anything like that. All the threading logic is taken care of for you, including telling the thread to stop if the activity has been closed. Just say what it is you want to do in the loadInBackground() method. Note that if you are developing for an API lower than 3.0, you can still access all the loaders via the Android Support Package.

like image 31
Kurtis Nusbaum Avatar answered Oct 02 '22 03:10

Kurtis Nusbaum