One way I know that is through activity.We can send data from fragment to activity and activity to fragment Is there any other way.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Quoting from the docs
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
I suggest you follow the method in the docs and i haven't tried any other alternative
For more info and example chekc the docs in the below link
http://developer.android.com/training/basics/fragments/communicating.html
Allowing fragments to communicate with each other by using activities as their intermediaries is a common best practice when using fragments. Visit http://developer.android.com/guide/components/fragments.html for more details on this important pattern. Anytime you need to interact with another fragment, you should always use a method in the fragment’s activity rather than access the other fragment directly. The only time it makes sense to access one fragment from another is when you know that you won’t need to reuse your fragment in another activity. You should almost always write fragments assuming that you’ll reuse them rather than hard-code them to each other.
Here is the solution,
Follow below steps:
1 create the interface like that
2.implements your activity using this interface
3.in this activity create on onUpdateTitle()
4.Now, In Fragment one write some code.
5.write some code in second Fragment setTitle()
In this solution when you click on button of Fragment one that time it shows value in second Fragment . I hope it will be helpful for you..!!
There are two methods I'd consider viable:
A.Communicate with your owning activity and forward messages to other fragments via that owning activity, details on that can be found int he official android documentation here:
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
Quote:
The Communication interface could look something like this:
The fragment would look something like this:
I am using a WeakReference here but that's really up to you. You can now use actionCallback to communicate with the owning activity and call the methods defined in IActionListener.
The owning Activity would look like this:
B. Now as for the second method - you can have fragments directly communicate with each other using interfaces as well - this way you don't have to know the exact class of the fragment you are talking to, which ensures loose coupling.
The setup is as follows: You have two fragments (or more) and an activity (to start the second fragment). We have an interface that lets Fragment 2 send a response to Fragment 1 once it has completed it's task. For the sake of simplicity we just re-use the interface I defined in A.
Here is our Fragment 1 :
Using the method described in A. Fragment 1 asks it's owning Activity to start Fragment 2. However, the Activity will pass along Fragment 1 as an argument to Fragment 2, so Fragment 2 can later indirectly access Fragment 1 and send the reply. Let's look at how the Activity preps Fragment 2:
Hint: I am using the Support-Framework, so if you develop solely for > Android 3.0 you can simply use FragmentActivity instead of ActionBarActivity.
Now FragmentTwo is being started, let's look at how FragmentTwo would communicate with FragmentOne:
Now the implementation of doSomething() in Fragment 1 will be called.
To pass data from one fragment to another
Bundle
will help.Then
push/call next Fragments.
and code to next Fragment: