Get current activity from fragment

后端 未结 2 1305
猫巷女王i
猫巷女王i 2021-02-19 05:37

I have this class for sending email with android

    public class SendMailTask extends AsyncTask {

    private ProgressDialog statusDialog;
    private Activity         


        
相关标签:
2条回答
  • 2021-02-19 06:25

    From fragment to activity use this :

    ((YourActivityName)getActivity()).yourPublicMethod();
    

    From activity to fragment use this :

    FragmentManager fragmentManager = getSupportFragmentManager();
    //include fragment via layout xml
    YourFragmentClass fragment = (YourFragmentClass)fragmentManager.findFragmentById(R.id.your_fragment_id);
    fragment.yourPublicMethod();
    
    0 讨论(0)
  • 2021-02-19 06:34

    Change below code

    new SendMailTask(MainActivity.this).execute(fromEmail,
                       fromPassword, toEmailList, emailSubject, emailBody);
    

    To

    new SendMailTask(getActivity()).execute(fromEmail,
                       fromPassword, toEmailList, emailSubject, emailBody);
    

    For more details you can check below link.

    Accessing the Activity from a Fragment

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