0

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;  
    }
};

}

allDroid
  • 405
  • 1
  • 10
  • 21
  • Search for **Anonymous Classes**. That's what `onSuccess` is here. – Codebender Aug 22 '15 at 03:25
  • @Codebender ,Thanks . i have one more doubt that is what onsuccess object return? ,as it is used as a variable parameter to pass in other function.when i googled about it i came to know that An object of Anonymous class is created that is referred by onSuccess reference variable of ICommand type.and what are those Void and String Parameters is it has to be same or what.could you explain me please. – allDroid Aug 22 '15 at 04:14

1 Answers1

0

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.

Max Pinto
  • 1,463
  • 3
  • 16
  • 29
  • Could u explain using some example.you have mentioned that it acts as a call back ,one more thing, in my code what onsuccess object return? ,as it is used as a variable parameter in other function. – allDroid Aug 22 '15 at 04:09
  • Well is a a big topic, try to search about android callbacks. And the object return could be a function and return data, or a procedure, doesnt return data, but internally make operations. Regards – Max Pinto Aug 22 '15 at 04:29
  • Thanks for you and this link http://stackoverflow.com/questions/2566852/callback-in-android which explain as per my requirement – allDroid Aug 22 '15 at 05:33