Transferred from: http://blog.csdn.net/jason0539/article/details/18075293
The first method:
Encountered a problem, need to control another acitivity in one activity to do some updates, did not expect to pass handler method, solve by the following way.
1. Defining attributes in MyApp handler
Package jason.com; Import Jason.com.MasterActivity.MyHandler; Import android.app.Application; /** * Implement application for data sharing * @author Jason * * * Public class MyAPP extends Application { //shared variable private Myhan Dler handler = null; Set method public void SetHandler (MyHandler handler) { This.handler = handler; } Get method public MyHandler GetHandler () { return handler; } }
2. Assigning the attribute handler to MyApp in the main activity
@Override public 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); Set Listener Btn_to.setonclicklistener (new Onclicklistener () { @Override public void OnClick (View v) { Set the shared variable mapp.sethandler (handler); Start another activity Intent Intent = new Intent (masteractivity.this, tochangeviewactivity.class); StartActivity (intent);}} ); }
3, in another activity to obtain the handler in MyApp to transmit value
protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.show); MAPP = (MyAPP) getapplication (); Get the shared variable instance Mhandler = Mapp.gethandler (); Findviewbyid (R.id.btn_chang). Setonclicklistener (New Onclicklistener () { @Override public void OnClick (View V) { //Send Message mhandler.sendemptymessage (CHANGED); ToChangeViewActivity.this.finish ();}} ); }
The second method:
Seeing the author's article was good, he turned around. Two activity can be broadcast to control another activity UI update, such as the group purchase app: After I placed an order, I can send a broadcast notification "My order" content content automatically updated.
Again, the problem I encountered is to open anotheractivity in the mainactivity to perform some actions, change some of the layout in the mainactivity, or perform some actions, The first thought is to mainactivity handler directly to anotheractivity, as if not feasible, there is this and the previous article.
In the previous scenario, by rewriting application to share the handler among the two activity, the solution today is to address the functions that would have been achieved by transmitting handler through a broadcast mechanism. is a workaround for transferring handler issues between activity,
In fact, it is very simple, is the application of broadcast, replaced the original want to solve the idea through sharing handler.
The code is as follows:
Mainactivity:
Package Jason.broadcastinsteadofhanlderdemo; Import android.app.Activity; Import Android.content.BroadcastReceiver; Import Android.content.Context; Import android.content.Intent; Import Android.content.IntentFilter; Import Android.os.Bundle; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; Import Android.widget.TextView; public class Mainactivity extends Activity {TextView TextView; Button Sbutton; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView = (TextView) Findviewbyid (r.id.show); Sbutton = (Button) Findviewbyid (R.id.startanother); Sbutton.setonclicklistener (New Onclicklistener () {@Override public void OnClick (V Iew v) {//TODO auto-generated Method Stub startactivity (new Intent (MAinactivity.this,anotheractivity.class)); } }); Intentfilter filter = new Intentfilter (anotheractivity.action); Registerreceiver (broadcastreceiver, filter); } broadcastreceiver broadcastreceiver = new Broadcastreceiver () {@Override public void onrecei ve (context context, Intent Intent) {//TODO auto-generated Method Stub Textview.settext (Intent. Getextras (). getString ("Data")); } }; protected void OnDestroy () {unregisterreceiver (broadcastreceiver); }; }
anotheractivity:
Package Jason.broadcastinsteadofhanlderdemo; Import android.app.Activity; Import android.content.Intent; Import Android.os.Bundle; Import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.Button; public class Anotheractivity extends Activity {public static final String action = "jason.broadcast.action"; Button Update; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub s Uper.oncreate (savedinstancestate); Setcontentview (R.layout.another); Update = (Button) Findviewbyid (R.id.updatemain); Update.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { TODO auto-generated Method Stub Intent Intent = new Intent (action); Intent.putextra ("Data", "Yes I am data"); Sendbroadcast (Intent); Finish (); } }); } }