Fragment and activity communication

FragmentManager provides a method similar to findViewById(), which is specially used to obtain instances of fragments from layout files. The code is as follows

RightFragment rightFragment=(RightFragment)getSupportFragmentManager().findFragmentById(R.id.right_fragment)

Call the findFragmentById() method of FragmentManager to get an instance of the corresponding fragment in the activity

In each fragment, the activity instance associated with the current fragment can be obtained by calling the getAvtivity() method. In
this way, the communication between the two is solved, which is very easy.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644291&siteId=291194637