Made as an example, before tabs, tabs are not needed:

https://habrahabr.ru/post/236567/

But there they did not suggest for beginners how to turn now to the elements in the fragment. I read the examples, tried it, but something goes wrong. Maybe the problem is that the examples for android above v4.

How now to address to elements which in a screen_one fragment? How, for example, process the NumberPicker and set the minimum and maximum values ​​and handle the OnValueChangeListener event from the mainActivity?

I understand that for beginners, but I can not understand. I read examples of the appeal from the main activity to the fragment, but it cannot be applied to this particular case ...

How to contact from the fragment itself, I figured out:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.screen_one, container, false); // numberPicker.findViewById(R.id.light_on_hour); NumberPicker light_on_hours = (NumberPicker) rootView.findViewById(R.id.light_on_hour); light_on_hours.setEnabled(true); light_on_hours.setMinValue(0); light_on_hours.setMaxValue(23); light_on_hours.setValue(10); return rootView; } 

Tell me how to contact the activity and listeners in the activity to intercept or transfer to the activity. And how to change the values ​​of the activity.

  • In general, it is not a good practice to do any business logic in activating. Read about MVP design patterns. And about your question, it’s not at all the best approach (for this and I don’t write it in the answers), in any fragment you can call the getActivity () method, which will give you the Activity object in which it is located. If you add it to a specific activation ((MainActivity) getActivity) .useSomeMethod (), you can call your root activation methods, send data there, etc. ... But I’ll say once again that doing business logic in activating is very "neochen" approach. - Eugene Troyanskii
  • You can also create a colbek in the activation and transfer it to a fragment when it is created. Or use third-party libraries like EventBus. - Eugene Troyanskii
  • @Eugene Troyanskii, I will clarify. I have a callback in MainActivity to which messages from the outside (mqtt) are received and processed, so if I receive a specific message, I need to change the visual element in the fragment. That is, I need to refer to the fragments from the MainActivity. Only here, as I understand it, the fragments in this example are dynamically loaded when a private void selectItem occurs (int position) {where is fragmentManager.beginTransaction () .replace (content_frame, fragment) .commit (); In general, can this be done in this case from MainActivity? - Vitali
  • It is possible through the fragment manager to access the fragment (if it is added to it). In general, when creating a fragment in avviti, you can save a link to its object (make a global variable) and then do whatever you want with it)) - Eugene Troyanskii
  • @ Eugene Troyanskii I understand everything here is written in detail, for people like me)))) developer.alexanderklimov.ru/android/fragment2.php - Vitali

1 answer 1

All figured out. If you bind to a specific example, the link to which is on top, then to call the View element from the fragment (example for NumberPicker):

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.screen_one, container, false); // numberPicker.findViewById(R.id.light_on_hour); NumberPicker light_on_hours = (NumberPicker) rootView.findViewById(R.id.light_on_hour); light_on_hours.setEnabled(true); light_on_hours.setMinValue(0); light_on_hours.setMaxValue(23); light_on_hours.setValue(10); return rootView; } 

If out of activity:

 FragmentManager fragmentManager = getSupportFragmentManager(); ScreenOne screen_one = (ScreenOne) fragmentManager.findFragmentById(R.id.content_frame); if (screen_one != null){ ((TextView) screen_one.getView().findViewById(R.id.text_light_on)) .setText("Access to Fragment 1 from Activity"); }