※ 引述《magic704226 (梅姬?没鸡?傻傻分不清楚)》之铭言:
: 目前有两个Activity
: A1 startActivity A2 后
: 还有资料要从 A1 -> A2
: 目前是用broadcast
: 有没有比较快的方法?
: 除了Android IPC binder实做?
Activity互相沟通可以透过以下方法
1. Intent
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("key", "value");
intent.putExtras(bundle);
intent.setClass(A.this, B.class);
startActivity(intent);
2. Broadcast
registerReceiver(mBroadcast, new IntentFilter(MY_MESSAGE));
Intent intent = new Intent();
intent.setAction(MY_MESSAGE);
sendBroadcast(intent);
3. Listener
public interface OnEventCompletedListener{
void onCompleted(String result);
}
public void setOnEventCompletedListener(OnEventCompletedListener listener){
mOnEventCompletedListener = listener;
}
private OnEventCompletedListener mOnEventCompletedListener;
public void notifyData(String result){
if(mOnEventCompletedListener != null){
mOnEventCompletedListener.onCompleted(result);
}
}
4. Application
public class MyApplication extends Application{
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((MyApplication)getApplication()).getStr();
}
}
5. singleton
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
6. EventBus
A Activity
MyEvent event = new MyEvent();
event.setMyEventString(editText.getText().toString());
mEventBus.post(event);
B Activity
public void onEventMainThread(MyEvent event){
event.getMyEventString();
}
靠印象打的 没有编译过 应该会有错
大guy4这几个方法