Vulture is an Android library that let you handle asynchronous callbacks properly within Activity/Fragment's life cycle.
Vulture frees you from the hassle with IllegalStateException
when coping with FragmentTransaction#commit()
after onSaveInstanceState.
Say you have fetchAsynchronously()
method to feth a message from a server asynchronously then call doCallback(message)
to show DialogFragment like below,
void fetchAsynchronously() {
/* do heavy asynchronous task here */
doCallback("finished!");
}
void doCallback(@NonNull String message) {
FinishDialog.newInstance().show(getSupportFragmentManager(), "TAG");
}
You simply annotate your Activity with @ObserveLifecycle
, then annotate your methods with @SafeCallback
that have to be callbacked safely like below.
Vulture automatically generates a class named SafeMainActivity
that has corresponding methods with @SafeCallback
annotations at compile time without any Reflection APIs.
@ObserveLifecycle
public class YourActivity extends AppCompatActivity {
void fetchAsynchronously() {
SafeMainActivity.doCallbackSafely("finished!");
}
@SafeCallback
void doCallback(@NonNull String message) {
FinishDialog.newInstance().show(getSupportFragmentManager(), "TAG");
}
}
When you call SafeMainActivity.doCallbackSafely(message)
after asynchronous task, the actual doCallback(message)
is called only within crash free window.
Finally, call register()/unregister()
methods to let Vulture know about the safe window.
@Override
protected void onResume() {
super.onResume();
SafeMainActivity.register(this);
}
@Override
protected void onPause() {
SafeMainActivity.unregister();
super.onPause();
}
Check sample
and generated codes for details.
annotationProcessor 'us.shiroyama.android:vulture-processor:0.3.0'
compile 'us.shiroyama.android:vulture:0.3.0'
Vulture supports argument types for methods annotated with @SafeCallback
listed below.
- All primitive types and its boxed types.
- All primitive array types (arrays of boxed types are NOT supported)
- String
- Bundle
- Serializable
- Parcelable
- ParcelableArray
- ParcelableArrayList
Vulture is based on the brilliant idea of PauseHandler
discussed at Stack Overflow. Thank you.
Copyright 2017 Fumihiko Shiroyama
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.