I want to change the text in text view in fragment1 by clicking a button in fragment2, i managed to do that by declaring the text view as static
so i can change the text by Fragment1.textv.setText("hi");
, is that ok and what is the best way to do this.
static textv ;
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_frag_beam_rec, container, false);
textv= (TextView)v.findViewById(R.id.textview);
return v;
}
Most common approach is using interfaces to 'speak' from fragment A to fragment B (you would need to communicate through the activity):
http://developer.android.com/training/basics/fragments/communicating.html
You can also use an Event-bus library like Otto, register the fragments that you want to update, and from the other fragment post an update:
如user3806331所述,您需要使用接口,单击链接即可找到所需的内容。但是请注意,静态引用窗口小部件是一种不好的做法,因为在某些情况下可能会导致内存泄漏。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.