简体   繁体   中英

what does this mean in native Android java.?

i am newbie in Android native development.while tracing code i found this code,but unable to understand what that does ..

My doubt in these is ,

1.)if we are using a interface methods in Activity.java ,shouldnt i make Activity.java to use implement keyword to implement those methods mentioned in Icommand Interface.

2.)If not then What is it doing here.is Variable onsuccess is an object or instance of interface?

please could anyone help me to understand this.

In Icommand.java

 public interface ICommand<T, S> {
public T execute(S params) throws Exception;
     }

In Activity.java

public class Activity extends BaseActivity {
 private ICommand<Void, String> onSuccess = new ICommand<Void, String>() {
    @Override
    public Void execute(String params) throws Exception {
        Activity.this.setPreferenceValue(Constants.PREF_PHONENUMBER, params);

        Activity.this.setPreferenceValue(Constants.PREF_HASPHONENUMBER, "true");
       Activity.this.finish();
        return null;  
    }
};

}

yes, you have to implement to your Interface, and Override methods you have there, thats the function.

It works as a Callback, so for example you start a new Thread, you can you the CallBack and pass to your Thread or AsyncTask and then in that new Class call the interface method. this automatically will call the activity method that you Override from your Interface. Very usefull to update some UI after some tasks.

Of course you can too use an internal class Callback Listener, but this is not recommendable. Code is more readable using implement and Overriding functions as well.

Hope thats help.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM