Sending data from Fragment to Activity in Android. Send objects from fragments to activity.

github code :

useful video :
https://youtu.be/nyuFac51Z6I

To send data from fragments to Activity we use interfaces.

TIP: Whichever activity that Implements an interface become a child class of that Interface.
& we can call a method defined inside an activity, inside a fragment and pass any data as per its parameters.

Step 1]

In the fragment class, create a public Interface, make a abstract method inside the interface.
PASS THE DATA YOU WANT TO SEND AS PARAMETER of the method.
(Data can be anything form variables to objects)

IMP : if you are passing an object , remeber to PARSE using the plugin or the Parceableber website  the object class.(Watch video).

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view= inflater.inflate(R.layout.fragment_detail, container, false);

fname_et = view.findViewById(R.id.fname_et);
lname_et = view.findViewById(R.id.lname_et);
age_et = view.findViewById(R.id.age_et);

Button button1 =view.findViewById(R.id.button2);

button1.setOnClickListener( v-> done());


return view;
}

//userdefined Interface
public interface Detailinterface {
void getDetaildata(Person person);
}
}




step2] Make any Activity that you want to send the data Implement you interface and override the interface methods


public class MainActivity extends AppCompatActivity implements DetailFragment.Detailinterface{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//Interface method
@Override
public void getDetaildata(Person person) {

TextView textView =findViewById(R.id.textview1);
textView.setText(person.fname + "\n");
textView.append(person.lname + "\n");
textView.append(String.valueOf(person.age));
}
}


Step3 ] Now we have to get a referenc to the Context of the Mainactivity to apply our interface method inside the fragment and passit the data.

Create a Variable whihc has a type of your Interface.


 */
public class DetailFragment extends Fragment {

EditText fname_et ;
EditText lname_et ;
EditText age_et ;


//A variable to store the context
private Detailinterface mlistner;


step4] Now override the onAttach() method of the fragment 
 
the 'context' hold the activity that is attached to the fragment.

Check if the Context that you are getting is an Instance of your interface, since the Activity implements the interface it is a Child class or instance of your interface.

if true then assign the Context to your variable that we made in step3.


@Override
public void onAttach(@NonNull Context context) {

if (context instanceof Detailinterface){
mlistner = (Detailinterface) context;
}
super.onAttach(context);
}


Step5] 
Now make an object (if you want to pass an object) or fetch the data you want send & call the interface method of the activity that you want to pass that data/object.

since your variable holds the context of the activity whihc has implemented our interface and overrided our method, we can call that same method and pass the object or Data as a parameter of that method.

private void done() {

Person person = new Person();
person.setFname(fname_et.getText().toString());
person.setLname(lname_et.getText().toString());
person.setAge(Integer.parseInt(age_et.getText().toString()));

mlistner.getDetaildata(person);
}


Inside the Mainactivity you can code what you want to do with that data inside the interface overrided method.



//Interface method
@Override
public void getDetaildata(Person person) {

TextView textView =findViewById(R.id.textview1);
textView.setText(person.fname + "\n");
textView.append(person.lname + "\n");
textView.append(String.valueOf(person.age));
}
}










Comments

Popular posts from this blog

React Js + React-Redux (part-2)

React Js + CSS Styling + React Router (part-1)

ViteJS (Module Bundlers, Build Tools)