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
| 123456789101112131415161718192021222324 |
packagejason.com; importjason.com.MasterActivity.MyHandler; importandroid.app.Application; /*** 自己实现Application,实现数据共享* @author jason*/publicclassMyAPP extendsApplication { // 共享变量 privateMyHandler handler = null; // set方法 publicvoidsetHandler(MyHandler handler) { this.handler = handler; } // get方法 publicMyHandler getHandler() { returnhandler; } } |
2. Assigning the attribute handler to MyApp in the main activity
| 1234567891011121314151617181920212223242526 |
@OverridepublicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mAPP = (MyAPP) getApplication(); handler = newMyHandler(); tv = (TextView) findViewById(R.id.tv); btn_to = (Button) findViewById(R.id.btn_to); // 设置监听器 btn_to.setOnClickListener(newOnClickListener() { @Overridepublic voidonClick(View v) { // 设置共享变量 mAPP.setHandler(handler); // 启动另一个Activity Intent intent = newIntent(MasterActivity.this, ToChangeViewActivity.class); startActivity(intent); } }); } |
3, in another activity to obtain the handler in MyApp to transmit value
| 123456789101112131415161718 |
protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show); mAPP = (MyAPP) getApplication(); // 获得该共享变量实例 mHandler = mAPP.getHandler(); findViewById(R.id.btn_chang).setOnClickListener(newOnClickListener() { @OverridepublicvoidonClick(View v) { // 发送消息 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:
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
packagejason.broadcastinsteadofhanlderdemo; importandroid.app.Activity; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.TextView; publicclassMainActivity extendsActivity { TextView textView; Button sButton; @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.show); sButton = (Button) findViewById(R.id.startAnother); sButton.setOnClickListener(newOnClickListener() { @Override publicvoid onClick(View v) { // TODO Auto-generated method stub startActivity(newIntent(MainActivity.this,AnotherActivity.class)); } }); IntentFilter filter = newIntentFilter(AnotherActivity.action); registerReceiver(broadcastReceiver, filter); } BroadcastReceiver broadcastReceiver = newBroadcastReceiver() { @Override publicvoidonReceive(Context context, Intent intent) { // TODO Auto-generated method stub textView.setText(intent.getExtras().getString("data")); } }; protectedvoid onDestroy() { unregisterReceiver(broadcastReceiver); }; } |
Anotheractivity:
| 12345678910111213141516171819202122232425262728293031323334 |
packagejason.broadcastinsteadofhanlderdemo; importandroid.app.Activity; import android.content.Intent; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; publicclassAnotherActivity extendsActivity { publicstaticfinalString action = "jason.broadcast.action"; Button update; @Override protectedvoidonCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.another); update = (Button) findViewById(R.id.updateMain); update.setOnClickListener(newOnClickListener() { @Override publicvoidonClick(View v) { // TODO Auto-generated method stub Intent intent = newIntent(action); intent.putExtra("data", "yes i am data"); sendBroadcast(intent); finish(); } }); } } |
Android Development controls UI updates for another activity in activity