Is the handler returning with no activity?

I am using a handler to update my UI after my thread - the following method is run from the thread (in a fragment)

public void signedIn(final boolean success, final String error) {
    Handler mainHandler = new Handler(getActivity().getMainLooper());
    mainHandler.post(new Runnable() {

        @Override
        public void run() {
            if(success) {
                Toast.makeText(getActivity(), "Signed In!",
                        Toast.LENGTH_SHORT).show();
            } else if (!success) {
                Toast.makeText(getActivity(), "Failed!" + error,
                        Toast.LENGTH_SHORT).show();
            }
        }
    });


}

      

This works great, however, if I return the phone at the correct time, the handler is called before the activity is created, so getActivity () returns null and the app crashes. I can't just wrap the method in if (getActivity ()! = Null), otherwise it won't update the interface at all.

What is the best approach to this problem? Does android have anything I can use to get around this?

Thank,

Kevin.

EDIT

So, I have applied one of the answers below, which seems to work. Using static reference to context in application class: Static way to get "Context" on Android?

I'm not sure about the details of using this, so a bit hesitant - I can post another question, seeing if there are any problems with this?

+3


source to share


3 answers


In this answer, you can take a look at what is the best practice for handling rotation related application configuration changes. The best way to deal with this is to create a snippet with setRetainInstance(true)

to keep a link to the current task.

This is necessary because when the screen rotates, Android destroys the current activity (even by calling the onDestroy method) and creates a new one with the new configuration. This way, you can know when the task is complete and trigger the UI activity with the activity still created.



EDIT

The snippet task should not push the results if the context is null, just save it. When the activity reloads, onCreate will be called. Since you used a task fragment, you will be able to get an instance of the fragment and see if this task has finished. The only thing that needs to be done is to restore its new state based on the already completed task of the fragment.

+1


source


Use this to get the looper:

Looper mainLooper = Looper.getMainLooper();

      

This works for me.



Edit: Looper.getMainLooper()

Returns the UI thread looper (main thread).

Edit2: You can use getContext()

in view to get context.

0


source


You can use getApplicationContext () to get the appropriate context for the Toast. However, the methods that you have access to are limited to where this code runs.

Edit:

I am trying to understand your methodology here. Why did you refuse to do this:

public void signedIn(boolean success, String error) {
    if(success){
        Toast.makeText(getActivity(), "Signed In!",
            Toast.LENGTH_SHORT).show();
    } 
    else{
        Toast.makeText(getActivity(), "Failed!" + error,
            Toast.LENGTH_SHORT).show();
    }
}

      

Enforcing asynchrony with the runnable created a potential race condition for accessing the activity instance. This seems like a completely unnecessary complexity for a simple toast display task.

Also, since booleans can only be in one of two states, you don't need a sentence else if

.

Also change the name of this method to better align with Java naming conventions. "signedIn" should be the name of a method that returns whether the user is signedIn.

0


source







All Articles