I am re-submitting this question, as I don't think my last one really went into the problem I was having.
So I have a thread that returns and then uses a handler to update the UI as the following:
public void completeSignIn(final boolean success, final String error) {
Log.d(Constants.LogTag, "Finalising Sign In...");
final Looper mainLooper = Looper.getMainLooper();
final Handler mainHandler = new Handler(mainLooper);
mainHandler.post(new Runnable() {
@Override
public void run() {
if (success) {
TextView tv = (TextView) getActivity().findViewById(R.id.register);
tv.setText("SIGNED IN!!");
} else if (!success) {
}
}
});
}
The problem I am having is that because the thread could return at any point after it is done querying the server, there is not gaurantee that getActivity() will return the Activity. I have found that if I rotate my device as the thread is about to return, this bit of code can be called inbetween the activities destroy/create cycle. I am unsure if it is important that I am using a fragment here, but I don't think there is any harm in using the parent activity to update views?
So I am unsure how I could force the handler to wait until the activity is created - is this possible, or is thee a standard way of dealing with this? As I don't see this in other apps I have tested.
UPDATE
I put some logs in my fragment and managed to get the following to illustrate my issue:
07-09 22:17:15.164 25435-25435/? D/Kevins_Tag﹕ Detaching Activity...
07-09 22:17:15.234 25435-26702/? D/Kevins_Tag﹕ Signing in...
07-09 22:17:15.234 25435-26702/? E/Kevins_Tag﹕ Activity is null
07-09 22:17:15.234 25435-26687/? D/Kevins_Tag﹕ Finalising Sign In...
07-09 22:17:15.284 25435-25435/? D/Kevins_Tag﹕ Attaching Activity...
07-09 22:17:15.284 25435-25435/? D/Kevins_Tag﹕ Activity Exists
As you can see, the thread is calling the UI in between detach and attach...