Viewer mode Android Update UI

Source: Internet
Author: User

Original URL: http://malong26.iteye.com/blog/1858724

Android app exits in several ways: Activity queue mode, broadcast mode. Here's a look at the way I'm implementing this feature-the observer pattern.
The approximate idea of the method is: Establish activity base class, implement observer interface, implement listening method, finish off this instance; In application, a single target object, when the program needs to be closed, call the target object's notification method, notify all observers.
The code is as follows:
Viewer interface

Java code
    1. Public interface Observer {
    2. public void Update (Subject Subject);
    3. }


Target object base class

Java code
  1. Public class Subject {
  2. private list<observer> observers = new arraylist<observer> ();
  3. public void Attach (Observer Observer) {
  4. OBSERVERS.ADD (Observer);
  5. }
  6. public void Detach (Observer Observer) {
  7. OBSERVERS.REMOVE (Observer);
  8. }
  9. protected void Notifyobservers () {
  10. For (Observer observer:observers) {
  11. Observer.update (this);
  12. }
  13. }
  14. }


Target object subclass (Implementation specific Exit method)

Java code
    1. Public class Applicationsubject extends Subject {
    2. public Void exit () {
    3. Notifyobservers ();
    4. }
    5. }


MyApplication Class (change the name of the label application to the fully qualified name of this class in the manifest file)

Java code
  1. Public class MyApplication extends application {
  2. private static Applicationsubject Applicationsubject;
  3. Public Applicationsubject Getapplicationsubject () {
  4. if (applicationsubject = = null) Applicationsubject = new Applicationsubject ();
  5. return applicationsubject;
  6. }
  7. }



Activity base class

Java code
  1. Public abstract class baseactivity extends Activity implements Observer {
  2. Public MyApplication app;
  3. @Override
  4. protected void OnCreate (Bundle savedinstancestate) {
  5. //TODO auto-generated method stub
  6. super.oncreate (savedinstancestate);
  7. App = (myapplication) this.getapplication ();
  8. App.getapplicationsubject (). Attach (this);
  9. }
  10. @Override
  11. public void Finish () {
  12. //TODO auto-generated method stub
  13. App.getapplicationsubject (). Detach (this);
  14. Super.finish ();
  15. }
  16. /** 
  17. * Close the program
  18. */
  19. public Void Close () {
  20. App.getapplicationsubject (). exit ();
  21. };
  22. @Override
  23. public void Update (Subject Subject) {
  24. //TODO auto-generated method stub
  25. This.finish ();
  26. }
  27. }



Write a few test pages to test

Java code
    1. Public class Atestactivity extends Baseactivity {
    2. @Override
    3. protected void OnCreate (Bundle savedinstancestate) {
    4. //TODO auto-generated method stub
    5. super.oncreate (savedinstancestate);
    6. StartActivity (new Intent (This, btestactivity.   Class));
    7. }
    8. }
    9. Public class Btestactivity extends Baseactivity {
    10. @Override
    11. protected void OnCreate (Bundle savedinstancestate) {
    12. //TODO auto-generated method stub
    13. super.oncreate (savedinstancestate);
    14. Close (); //Call the Close method in Btestactivity and find that both A and B are closed
    15. }
    16. }

Viewer mode Android Update UI

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.