?????????????????????????й?
???????????? ???????[ 2016/12/15 16:57:43 ] ???????????й? ???????
??????????????????????????????????н?????
????????????????????????????????????????????鷳???
?????????????????????????????????????WeakReference????????????????????й??????Ρ?
?????????????? Enrique López Ma?as ??????Google Developers Experts????????????
????Finally understanding how references work in Android and Java
????A few weeks ago I attended Mobiconf?? one of the best conferences for Mobile Developers I had the pleasure to attend in…medium.com
?????????????Java???????????
??????????2??????????????????????????и??????????????????????????????????????????????ò??????????á?
?????????????????ò????????????????
?????????????????????ò???????????????????????????????й?????????????ζ??????????????
????????????и?????????????????????й??????????????????????????????????????????????б??????й????????
????????AsyncTask???й?????????
????Activity??
public class MainActivity extends Activity {
private MyAsyncTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
task = new MyAsyncTask();
task.setListener(createListener());
task.execute();
}
@Override
protected void onDestroy() {
task.setListener(null);
super.onDestroy();
}
private MyAsyncTask.Listener createListener() {
return new MyAsyncTask.Listener() {
@Override
public void onSuccess(Object object) {
// adapt contents
}
};
}
}
??????????AsyncTask:
classMyAsyncTaskextendsAsyncTask{
privateListenerlistener;
@Override
protectedObjectdoInBackground(Object[]params){
returndoSomeStuff();
}
privateObjectdoSomeStuff(){
//dosomethingtogetresult
returnnewObject();
}
@Override
protectedvoidonPostExecute(Objectobject){
if(listener!=null){
listener.onSuccess(object);
}
}
publicvoidsetListener(Listenerlistener){
this.listener=listener;
}
interfaceListener{
voidonSuccess(Objectobject);
}
}
??????????????????????????????????????????????????????????
??????????????????RxJava????????????????????????????á?
public class MainActivity extends Activity {
private Subscription subscription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
subscription = Observable
.fromCallable(new Callable<Object>() {
@Override
public Object call() throws Exception {
return doSomeStuff();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Object>() {
@Override
public void call(Object o) {
// adapt contents
}
});
}
private Object doSomeStuff() {
//do something to get result
return new Object();
}
@Override
protected void onDestroy() {
subscription.unsubscribe();
super.onDestroy();
}
}
?????????????????unsubscribe Subscription?????????????????й???
???????????????Novoda????????????????????????????μ????????????????κ????????????
????novoda/bonfire-firebase-sample
????bonfire-firebase-sample - An app to discuss your favourite emojis. This is a sample app built with Firebase.github.com
????novoda/spikes
????spikes - Where ideas & concepts are born & incubatedgithub.com
????????????????????????????????????????????????????????????????????????????????????????????????????????
?????÷????????????????????????????????????????????????С?
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11