How to cause activity from another fragment.?

I already know how to make a snippet in Android. I want to open another action in the same fragment when the button is clicked. The button is inside the fragment class.

How to do it?

+6
source share
2 answers

Depending on what you need, there are two possibilities:

  • The result of obtaining a fragment that demonstrates the beginning of a new action from the fragment and receiving the result from it.
  • setTargetFragment can be used, for example, if this fragment is launched by another, and when it wants to return the result to the first. An example is available here.
+2
source

The connection between fragments and fragments is carried out through the activity associated with it. Two fragments should never bind directly.

Here are some example guides

EDIT: April 2013

I like the comment by @AlexLockwood.

In the case when one fragment starts another fragment, it is fine to use setTargetFragment(Fragment, int) and directly contact it by calling getTargetFragment() . Communicating with Fragments through an activity is usually a good idea, because it keeps your fragments loosely connected to each other.

0
source

Source: https://habr.com/ru/post/892823/


All Articles