Using callbacks in Activity
We have a Webservice class that takes a WebserviceListener as the callback for that specific webservice call. My problem is that Android can recreate my Activity (orientation change, etc...), and then I have the old references in that callback. When I try to set the visibility of a view, or set my hidden flag to vai saveInstanceState
method, they are all recreated. How should I address that problem?
The code I have:
public class UploadActivity extends Activity implements {
private Button mButton;
private volatile boolean mHidden;
private class UploadWSListener extends WebServiceAdapter {
@Override
public void onComplete(Bundle bundle) {
mSuggestion = (Suggestion) bundle.getSerializable(WebService.BUNDLE_DATA);
if (mSuggestion.isAutomatic()) {
myHandler.sendEmptyMessage(1);
}
else {
myHandler.sendEmptyMessage(0);
}
}
@Override
public void onServerError(ErrorResult error, Bundle bundle) {
onError(error.getDebugInfo());
}
@Override
public void onError(WebServiceException wse, Bundle bundle) {
onError(wse.getMessage());
}
private void onError(String message) {
Log.w(TAG, "Error downloading suggestions. Error: " + message);
myHandler.sendEmptyMessage(-1);
}
}
private class PhotoUploadHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "Handler is ivoked. What: " + msg.what);
switch (msg.what) {
开发者_运维问答 case 0:
mButton.setText("Upload");
break;
case 1:
mButton.setVisibility(View.GONE);
mHidden = true;
break;
case -1:
Toast.makeText(PhotoUploadActivity.this, "Network error", Toast.LENGTH_LONG).show();
finish();
break;
}
Log.i(TAG, "Handler with " + msg.what + " run.");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.i(TAG, "onSaveInstanceState hidden:" + mHidden);
super.onSaveInstanceState(outState);
// outState.putBoolean("hide", mButton.getVisibility() == View.GONE);
outState.putBoolean("hide", mHidden);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState != null && savedInstanceState.getBoolean("hide")) {
mButton.setVisibility(View.GONE);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myHandler = new UploadHandler();
Model model = Model.instance(getApplicationContext());
WebService service = model.getWebService();
if (Model.u) {
Model.u = false;
service.getSuggestions(new UploadWSListener(), Prefs.getUserToken(getApplicationContext()), getLatitude(),getLongitude(), getAltitude());
}
}
}
My main questions:
- How can I use this callback design with an Activity? (I'm pretty much stuck with the design)
- Is it even smart to hold Views as instance variables in my Activity, if they are recreated unpredictably, or just get them via
findViewByID
?
I think you need a way to register you activity to and from the listener doing this you can react on orientation changes by unregistering the activity and registering it again when the new activity is created. As far as I know between the destruction and recreation all calls to the "old" activity will be retained till the new activity was created.
Holding views in your activity is ok as they will be released/destructed when your activity gets destroyed. If you have to access them multiple times it's ok to hold them as a class member as getting them via findViewByID() can be expensive when you layout is very deep structured.
精彩评论