尝试使用意图在活动之间进行通信
通过 onNewIntent:
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
if (query == null) {
query = intent.getData().toString();
}
doMySearch(query);
}
}
或使用监听器。
class ActivityA implements Activity {
// Nested 'listener'
protected class TitleBarListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(NEW_TITLE_INTENT))
{
intent.getStringExtra(NEW_TITLE_TEXT_VALUE));
}
}
}
TitleBarListener mListener;
// then create and register
mListener = new TitleBarListener();
registerReceiver(mListener, new IntentFilter(NEW_TITLE_INTENT));
确保将意图添加到 android.xml
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
<action android:name="com.mypackage.changeTitle"/>
</intent-filter>
那么您应该能够向该活动广播/发送意图
Intent i = new Intent(AccountMainView.NEW_TITLE_INTENT);
getActivity().sendBroadcast(i);
// or if you activity is singleTop and you're using the onNewIntent:
Intent i=new Intent(this,MainActivity.class);
i.putExtra("methodName","Mymethod");//goes to previous Intent
startActivity(i);//will trigger only Mymethod in MainActivity