简体   繁体   中英

How to update Textview of an DialogFragment from another Fragment

First Fragment RequestDialog

when I called function request_services_change by same class is working fine but when call by FragmentB is a null pointer and request_services is empty on debugger

 public void RequestDialog(Context context) {

        this.listener = listener;
    }


 public RequestDialog()
    {

    }

  public void request_services_change()
    {
                    Log.d("Text","Request Dailog First Fragment ");// this is working fine from called by same class or Fragment B
        request_services.setText("hello from test 2"); // is called by same class is working fine but when call by FragmentB is null pointer and request_services is empty on debuger 
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.view = inflater.inflate(R.layout.add_request_dailoge, container, true);
         request_services = (TextView) view.findViewById(R.id.services_request);

        return this.view;
    }

Fragment FragmentB

private RequestDialog listener;
public FragmentB () {
    this.listener=new RequestDialog();
}

public function Test()
{
   listener.View_request_services();
}

Although there are a bunch of other ways to do this but a particular library works just fine for me: Event bus .

Event bus facilitates and simplifies communication between Activities, Fragments, Threads, Services, etc. In your case, you can use this library to update the TextView of your DialogFragment (even from another fragment).

It also has extra features like delivery threads and it is fast and relatively light too. I hope this helps.. Merry coding!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM