※ 引述《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這幾個方法