I'm pretty new in android development and I have some questions about how fragments communicate with their main activity.
How can i use a widget defined in my fragment without the Onclick
method,
i want to do something like:
in my MainActivity
textview = (textview) findbyid(R.id.textview)
when the textview is defined in fragment.
i'm working on a project in rosjava
that's why i have to get the widget i defined in my fragment in order to execute them as ros node
.
You could declare Fragment member and instantiate it in the Activity.
// Fragment.java
...
public TextView textview;
...
void onCreateView(...) {
//inflating view
...
textview = view.findViewbyId(R.id.textview);
...
//Activity.java
private MyFragment fragment;
...
fragment = MyFragment.newInstance();
// now u could do like that
fragment.textview.setText("hello");
Or u can get ur fragment object using FragmentManager:
Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_container);
if (fragment instanceof MyFragment) {
MyFragment myfragemnt = (MyFragment) fragment;
fragment.textview.setText("hello");
}