Today i will introduce pass data Activity and Fragment in Android, this article I use a transfer mode data in programming Android, if you do not know about the Event bus Otto, learn about it then, then I will guide you to use Otto eventbus pass data activity to fragment in android.
Here are the steps to perform data send in android.
Here are the steps to perform data send in android.
Step 1. import libary otto event bus
add in file Gradledependencies {
compile 'com.squareup:otto:1.3.8'
}
Step 2. Create layout main_activity.xml and fragment_layout.xml
main_activity.xml<?xml version="1.0" encoding="utf-8"?>Fragment_layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cfcfcf">
<RelativeLayout
android:id="@+id/activityLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity"
android:textStyle="bold"/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"/>
<EditText
android:id="@+id/activityData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/message"
android:hint="pass data"/>
<Button
android:id="@+id/sendMessageToFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/activityData"
android:layout_centerHorizontal="true"
android:onClick="sendMessageToFragment"
android:text="Send Message To Fragment"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/activityLayout"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment"
android:textStyle="bold"/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/message"
android:hint="Pass Data"/>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:text="Send message to Activity"/>
</RelativeLayout>
Step 3. Create class OttoBus
public class OttoBus {
private static Bus sBus;
public static Bus getBus() {
if (sBus == null)
sBus = new Bus();
return sBus;
}
}
Step 4. Create Events Class when pass data in android
public class Events {
public static class FragmentActivityMessage {
private String message;
public FragmentActivityMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
public static class ActivityFragmentMessage {
private String message;
public ActivityFragmentMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
}
Step 5. Create Main.java
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addFragment();
}
@Override
protected void onStart() {
super.onStart();
OttoBus.getBus().register(this);
}
private void addFragment() {
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, new FragmentExample()).commit();
}
public void sendMessageToFragment(View view) {
EditText etMessage = findViewById(R.id.activityData);
OttoBus.getBus().post(String.valueOf(etMessage.getText()));
}
@Subscribe
public void getMessage(Events.ActivityFragmentMessage message) {
TextView messageView = findViewById(R.id.message);
messageView.setText(message.getMessage());
}
@Override
protected void onStop() {
super.onStop();
OttoBus.getBus().unregister(this);
}
}
Step 6. Create Fragment.java
public class FragmentExample extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.demo_fragment, container, false);
}
@Override
public void onStart() {
super.onStart();
OttoBus.getBus().register(this);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setClickListener(view);
}
public void setClickListener(final View view) {
Button btnSubmit = (Button) view.findViewById(R.id.submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessageToActivity();
}
});
}
private void sendMessageToActivity() {
EditText etMessage = getView().findViewById(R.id.editText);
OttoBus.getBus().post(etMessage.getText().toString());
}
@Subscribe
public void getMessage(Events.FragmentActivityMessage message) {
TextView messageView = getView().findViewById(R.id.message);
messageView.setText(message.getMessage());
}
@Override
public void onStop() {
super.onStop();
OttoBus.getBus().unregister(this);
}
}
No comments:
Post a Comment