Skip to content

Instantly share code, notes, and snippets.

@salihyalcin
Created February 18, 2015 16:29
Show Gist options
  • Save salihyalcin/f41254ce7c7c8d8c1539 to your computer and use it in GitHub Desktop.
Save salihyalcin/f41254ce7c7c8d8c1539 to your computer and use it in GitHub Desktop.
InterFragment Communication
package example.fragmentcommu;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class FragmentOne extends Fragment {
SendMessage sm;
Button btn;
EditText et;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rotview = inflater.inflate(R.layout.fragmentone,container,false);
btn = (Button) rotview.findViewById(R.id.button);
et = (EditText) rotview.findViewById(R.id.msg);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Message;
Message = et.getText().toString();
sm.sendData(Message);
}
});
return rotview;
}
interface SendMessage
{
public void sendData (String message);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
sm = (SendMessage) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException("You need to implement sendData Method");
}
}
}
package example.fragmentcommu;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentTwo extends Fragment {
TextView txt;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rotview = inflater.inflate(R.layout.fragmenttwo,container,false);
txt = (TextView)rotview.findViewById(R.id.txt);
return rotview;
}
public void getData (String message) {
txt.setText(message);
}
}
package example.fragmentcommu;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity implements FragmentOne.SendMessage {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void sendData(String message) {
FragmentTwo f2 = (FragmentTwo)getSupportFragmentManager().findFragmentById(R.id.fragment2);
f2.getData(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment