1) ChildActivity.java
package eventbusdemo.credencys.com.eventbusdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.greenrobot.eventbus.EventBus;
public class ChildActivity extends AppCompatActivity implements View.OnClickListener {
private EditText messageEditText;
private Button triggerEventButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
messageEditText = (EditText) findViewById(R.id.activity_child_edt_message);
triggerEventButton = (Button) findViewById(R.id.activity_child_btn_trigger);
triggerEventButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.activity_child_btn_trigger:
String message = messageEditText.getText().toString().trim();
CustomMessageEvent event = new CustomMessageEvent();
event.setCustomMsg(message);
EventBus.getDefault().post(event);
finish();
break;
}
}
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "eventbusdemo.credencys.com.eventbusdemo"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.android.support:design:25.1.0'
testCompile 'junit:junit:4.12'
}
package eventbusdemo.credencys.com.eventbusdemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.greenrobot.eventbus.EventBus;
public class ChildActivity extends AppCompatActivity implements View.OnClickListener {
private EditText messageEditText;
private Button triggerEventButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
messageEditText = (EditText) findViewById(R.id.activity_child_edt_message);
triggerEventButton = (Button) findViewById(R.id.activity_child_btn_trigger);
triggerEventButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.activity_child_btn_trigger:
String message = messageEditText.getText().toString().trim();
CustomMessageEvent event = new CustomMessageEvent();
event.setCustomMsg(message);
EventBus.getDefault().post(event);
finish();
break;
}
}
}
2)CustomMessageEvent .java
package eventbusdemo.credencys.com.eventbusdemo;
/**
* Created by manisha on 20/2/17.
*/
public class CustomMessageEvent {
public String customMsg;
public String getCustomMsg() {
return customMsg;
}
public void setCustomMsg(String customMsg) {
this.customMsg = customMsg;
}
}
3)MainActivity .java
package eventbusdemo.credencys.com.eventbusdemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText resultsEditText;
private Button launchButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultsEditText = (EditText)findViewById(R.id.activity_main_edt_result);
launchButton = (Button)findViewById(R.id.activity_main_btn_launch);
launchButton.setOnClickListener(this);
EventBus.getDefault().register(this);
}
@Subscribe
public void onEvent(CustomMessageEvent event){
Log.e("","Event bus::"+event.getCustomMsg());
resultsEditText.setText(event.getCustomMsg());
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.activity_main_btn_launch:
Intent intent = new Intent(MainActivity.this,ChildActivity.class);
startActivity(intent);
break;
}
}
}
Gradle
apply plugin:
'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "eventbusdemo.credencys.com.eventbusdemo"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.android.support:design:25.1.0'
testCompile 'junit:junit:4.12'
}
No comments:
Post a Comment