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)