Android development: Controlling the UI update of another activity in the activity _ How to pass handle between activities

Source: Internet
Author: User

If you encounter a problem, you need to control the other acitivity in an activity for some updates. Instead of passing the handler method, you can solve it in the following ways.

1. Define the property handler in MyAPP

Package jason.com; import jason.com. masterActivity. myHandler; import android. app. application;/*** implement Application by yourself to achieve data sharing * @ author jason */public class MyAPP extends Application {// sharing variable private MyHandler handler = null; // set method public void setHandler (MyHandler handler) {this. handler = handler;} // get method public MyHandler getHandler () {return handler ;}}


2. assign a value to the handler of MyAPP in the main activity.

@ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mAPP = (MyAPP) getApplication (); handler = new MyHandler (); TV = (TextView) findViewById (R. id. TV); btn_to = (Button) findViewById (R. id. btn_to); // sets the listener btn_to.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// sets the sharing variable mAPP. setHandler (handler); // start another ActivityIntent intent = new Intent (MasterActivity. this, ToChangeViewActivity. class); startActivity (intent );}});}


3. Get handler in MyAPP in another activity to pass the value

Protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. show); mAPP = (MyAPP) getApplication (); // obtain the shared variable instance mHandler = mAPP. getHandler (); findViewById (R. id. btn_chang ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// send message mHandler. sendEmptyMessage (CHANGED); ToChangeViewActivity. this. finish ();}});}

Author: jason0539

Weibo: http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)


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.