添加一个eventbus 3.0 jar包
MainActivity:
public class MainActivity extends AppCompatActivity { private Button button; private TextView te; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // //注册EventBus EventBus.getDefault().register(this); button = (Button) findViewById(R.id.bu); te = (TextView) findViewById(R.id.te); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Second_activity.class); startActivity(intent); } }); } //反注册EventBus @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void getZhi(String str){ te.setText(str); } }
second_Activity:
public class Second_activity extends AppCompatActivity { private Button button; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.er_item); button = (Button) findViewById(R.id.chuanzhi); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post("岳俊飞"); } }); } }