I have a simple app that has two fragments and when in landscape mode, both fragments are shown side by side and in portrait I show Fragment A and then if they select an option, start an Activity that shows Fragment B. My problem is when I am in Portrait mode and showing Fragment B, if the user selects a menu option I want to refresh or redraw the View that is associated with Fragment B and can’t understand how to make this work. I tried the getView method and getLayoutInflater method but the screen doesn’t change because I think I am creating a new view. I also tried to get a reference to Fragment A thinking I could call its routine to build a new fragment and replace Fragment B but can’t get a reference to it because it is not being displayed. What I really need to do is just force the onCreateView method to be called again to inflate the new view but I have tried several methods to try to do this but can’t get the onCreateView to be called again. Any thoughts on how to this?
相关问题
- 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
In
onOptionsItemSelected()
, have the activity call a method on the fragment that causes it to update its widgets with the new content. Or, have the activity execute aFragmentTransaction
to replace the fragment (if the fragment was originally set up via aFragmentTransaction
).You will want to perform a
FragmentTransaction
and use thereplace()
methodThis shouldn't be too hard to do, but the answer will depend on where your Menu is located (i.e. is your
onOptionsItemSelected()
call back inside your parent Activity or is it in either Fragment A/B?).Suppose for simplicity's sake, your menu implementation and
onOptionsItemSelected()
is in the parent activity, and you want to reshape the fragments in the event that menu_option1 is chosen. It would look something like this:Alternatively, if your menu is a child of one of your Fragments (which it should be for the sake of more reusable code), then one method is to require that host Activity to implement an interface defined by the Fragment, that can be used as a call back. And in the
onOptionsItemSelected()
callback inside your fragment class, you simply make a call to this callback method.Although it sounds like a mouthful, you only really need to do a couple of things. Pretend that this is your Fragment class
Then in the Activity, you would see something like: