代码块:
public class MainActivity extends AppCompatActivity {
EventBus bus;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bus=EventBus.getDefault();
bus.register(this);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
}
public void click(View view){
/*bus.post("我就是EventBus发送的消息");
*/
Intent intent=new Intent(this,Main2Activity.class);
startActivity(intent);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void MyThread(String par){
Log.i("aaa","aa:"+par);
Toast.makeText(MainActivity.this,"第一个页面的:"+par,Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
bus.unregister(this);
}
}
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void click(View view){
EventBus.getDefault().post("我来了");
finish();
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="org.frxm.eventbus.MainActivity">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:max="100"
android:progress="0"
style="?android:progressBarStyleHorizontal"/>
<Button
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="发送消息"
android:onClick="click"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.frxm.eventbus.Main2Activity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="发送"/>
</RelativeLayout>