How can I send information of activity to fragment and of fragment to activity in android?
Asked
Active
Viewed 127 times
-2
1 Answers
2
Your question is very BAD and not clear! Please before ask any question, first read this link then ask question.
But for use custom method in Activity
from Fragment
, you can use my below example.
First write your Method in your activity:
public void showMessage(){
Toast.makeText(context, "YOUR MESSAGE", Toast.LENGTH_SHORT).show();
}
Then, you should create instance of activity in your Fragment :
public class YOUR_FRAGMENT_NAME extends Fragment {
private YOUR_ACTIIVTY_NAME yourActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_layout, container, false);
//Initialize
mainActivity = (MainActivity) getActivity();
//Call method
yourActivity.showMessage();
return view;
}
I hope help you.

Mohammad Nouri
- 2,245
- 4
- 17
- 34
-
thanks a lot Engineer for Best answer – Marziyeh Yegani Oct 09 '17 at 11:31