How to understand the activity in Android callback _android

Source: Internet
Author: User

Why do I need a method callback?

Method callback is a means of functional definition and function separation, and it is a loosely coupled design idea. In Java, callbacks are implemented through interfaces. As a system architecture, you must have your own running environment, and provide the user's implementation interface.

Here's an example to simulate the method of activity in Android to recall the idea.
Activity Interface

Copy Code code as follows:

Package Com.xujing.test
Defining interfaces
public interface activity{
method to call when creating
public void onCreate ();
method to invoke at startup
public void OnStart ();
method to call when destroying
public void ondestory ();
}

Implementation class myactivity of activity interface

Copy Code code as follows:

Package Com.xujing.test
Define a class to implement an Activity interface
public void MyActivity Implements activity{
Implementation creation method, simple output hint information
@Override
public void OnCreate () {
System.out.println ("OnCreate ...");
}

Implementation startup method, simple output hint information
@Override
public void OnStart () {
System.out.println ("OnStart ...");
}

Implement the destruction method, simple output hint information
@Override
public void Ondestory () {
System.out.println ("Ondestory ...");
}
}

System running Environment Class Androidsystem

Copy Code code as follows:

Package Com.xujing.test
System running Environment class
public class androidsystem{
Define Create constants
public static final int create=1;
Defining startup Constants
public static final int start=2;
Define destroy constants
public static final int destory=3;

How to Run
public void run (activity a,int state) {
Switch (state) {
Create
Case CREATE:
A.oncreate ();
Break
Start
Case START:
A.onstart ();
Break
Destroyed
Case Destory:
A.ondestory ();
Break
}
}
}

Test class:

Copy Code code as follows:

Package Com.xujing.test
Test class
public class test{
Main method
public static void Main (string[] args) {
Instantiation of Androidsystem
Androidsystem system = new Androidsystem ();

Instantiation of MyActivity
Activity A = new myactivity ();

Create
System.run (a,androidsystem.create);
Start
System.run (A,androidsystem.start);
Destroyed
System.run (a,androidsystem.destory);
}
}

Through the above code we can see that the interface (System framework) is provided by the system, the implementation of the interface is user-implemented. This can achieve interface unification, achieve different. The system achieves the separation of interfaces and implementations by "callback" our implementation classes in different states.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.