I have two classes. First is activity, second is a fragment where I have some EditText
. In activity I have a subclass with async-task and in method doInBackground
I get some result, which I save to variable. How can I send this variable from subclass "my activity" to this fragment?
相关问题
- 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
Use following interface to communicate between activity and fragment
Or use following this generic listener for two way communication using interface
fragment show method
you can cast your context to
GenericConnector
inonAttach(Context)
tooin your activity
in your fragment
Note: Never use it like
"".toString().toString().toString();
way.My solution is to write a static method inside the fragment:
This way I am sure that all the data I need is inside the Fragment before any other possible operation which could need to work with it. Also it looks cleaner in my opinion.
Smartest tried and tested way of passing data between fragments and activity is to create a variables,example:
Then to pass data from fragment to activity, we do so in the onActivityCreated method:
Now you can get the value of StorageUtil.employees from everywhere. Goodluck!
Very old post, still I dare to add a little explanation that would had been helpful for me.
Technically you can directly set members of any type in a fragment from activity.
So why Bundle?
The reason is very simple - Bundle provides uniform way to handle:
-- creating/opening fragment
-- reconfiguration (screen rotation) - just add initial/updated bundle to outState in onSaveInstanceState()
-- app restoration after being garbage collected in background (as with reconfiguration).
You can (if you like experiments) create a workaround in simple situations but Bundle-approach just doesn't see difference between one fragment and one thousand on a backstack - it stays simple and straightforward.
That's why the answer by @Elenasys is the most elegant and universal solution.
And that's why the answer given by @Martin has pitfalls
In your activity declare static variable
Then in your fragment do like follow
I´ve found a lot of answers here @ stackoverflow.com but definitely this is the correct answer of:
"Sending data from activity to fragment in android".
Activity:
Fragment:
Reading the value in the fragment
or just