Classic teaches that activity should not directly interact with fragment methods and variables. Only through interfaces. When studying this topic, I noticed that the transfer of data from the activity to the fragment, carried out through Bandle, refers directly to the field of the fragment in the activity. How does this fit the teaching?

@Override public void interface1(String string) { Fragment fragment = new Fragment2(); FragmentManager fm = getFragmentManager(); Bundle bundle = new Bundle(); bundle.putString(Fragment2.KEY, string); //Fragment2.KEY - поле фрагмента fragment.setArguments(bundle); fm.beginTransaction().add(R.id.fragmentContainer2, fragment).commit(); } 
  • This is the standard way to pass arguments. You can make an interface in which there will be constants for the keys of the bundle. - Ruslan Yagupov
  • four
    You are confused. All the way around. Fragments do not need to know anything about activations, but activations know everything about fragments that are hosted. - Yura Ivanov
  • The constant interface is anti-puppy. Instead, it is better to use a constant abstract class or enum. - Nikolay
  • How is an abstract class better than an interface ?, and enum inflates apk. - Ruslan Yagupov
  • You can read the book Effective Java, in which this anti-pattern is considered. Or here's the relevant question from enSO - stackoverflow.com/questions/2659593/… - Nikolay

1 answer 1

As I understand it, the essence of the approach that you read in the books is aimed at protecting data in the fields of objects from being cleared, which can occur in Android under a variety of circumstances. And any value that was initialized in an object (Activity) is not insured against loss when it is accessed from another application element (Fragment). And in your example, only a static key-identifier is used, which is not going anywhere, no matter how much the application is folded-deployed