Android Navigation Drawer unable to setText in a Fragment from MainActivity

后端 未结 2 796
自闭症患者
自闭症患者 2020-12-22 10:49

I am trying to put GPS coordinates on TextView of a Fragment from MainActivity but I can not figure out how to do this.

I have:

MainActivity.java
Loc         


        
相关标签:
2条回答
  • 2020-12-22 11:34

    its very simple, just create one method in fragment like below,

    public void SetTitle() {
            text_title.setText("hello fragment");
        }
    

    now call that method from Activity, below code it for <= ANDROID API 11

    HomeFragment fragment = (HomeFragment) getSupportFragmentManager()
                            .findFragmentByTag("HomeFragment");
                    fragment.SetTitle();
    

    for > 11 API just change getFragmentManager()

    now you can do whatever you want.

    0 讨论(0)
  • 2020-12-22 11:41

    You can use Interface for it so main objective of Fragment re-usability is maintained. You can implement communication between Activity-Fragment OR Fragment-Fragment via using following :

    enter image description here

    0 讨论(0)
提交回复
热议问题