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 |
package
jason.com;
import
jason.com.MasterActivity.MyHandler;
import
android.app.Application;
/**
* 自己实现Application,实现数据共享
* @author jason
*/
public
class
MyAPP
extends
Application {
// 共享变量
private
MyHandler handler =
null
;
// set方法
public
void
setHandler(MyHandler handler) {
this
.handler = handler;
}
// get方法
public
MyHandler getHandler() {
return
handler;
}
}
|
2. Assigning the attribute handler to MyApp in the main activity
1234567891011121314151617181920212223242526 |
@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);
// 设置监听器
btn_to.setOnClickListener(
new
OnClickListener() {
@Override
public void
onClick(View v) {
// 设置共享变量
mAPP.setHandler(handler);
// 启动另一个Activity
Intent intent =
new
Intent(MasterActivity.
this
,
ToChangeViewActivity.
class
);
startActivity(intent);
}
});
}
|
3, in another activity to obtain the handler in MyApp to transmit value
123456789101112131415161718 |
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.show);
mAPP = (MyAPP) getApplication();
// 获得该共享变量实例
mHandler = mAPP.getHandler();
findViewById(R.id.btn_chang).setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(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 |
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(View 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
onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
textView.setText(intent.getExtras().getString(
"data"
));
}
};
protected
void onDestroy() {
unregisterReceiver(broadcastReceiver);
};
}
|
Anotheractivity:
12345678910111213141516171819202122232425262728293031323334 |
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
super
.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();
}
});
}
}
|
Android Development controls UI updates for another activity in activity