I have this class for sending email with android
public class SendMailTask extends AsyncTask {
private ProgressDialog statusDialog;
private Activity sendMailActivity;
public SendMailTask(Activity activity) {
sendMailActivity = activity;
}
protected void onPreExecute() {
statusDialog = new ProgressDialog(sendMailActivity);
statusDialog.setMessage("Getting ready...");
statusDialog.setIndeterminate(false);
statusDialog.setCancelable(false);
statusDialog.show();
}
@Override
protected Object doInBackground(Object... args) {
try {
Log.i("SendMailTask", "About to instantiate GMail...");
publishProgress("Processing input....");
GMail androidEmail = new GMail(args[0].toString(),
args[1].toString(), (List) args[2], args[3].toString(),
args[4].toString());
publishProgress("Preparing mail message....");
androidEmail.createEmailMessage();
publishProgress("Invio email in corso, sarai ricontattato da un nostro operatore per la conferma");
androidEmail.sendEmail();
publishProgress("Email Sent.");
Log.i("SendMailTask", "Mail Sent.");
} catch (Exception e) {
publishProgress(e.getMessage());
Log.e("SendMailTask", e.getMessage(), e);
}
return null;
}
@Override
public void onProgressUpdate(Object... values) {
statusDialog.setMessage(values[0].toString());
}
@Override
public void onPostExecute(Object result) {
statusDialog.dismiss();
}
}
Without using fragments i use the following code in the MainActivity to send a mail
new SendMailTask(MainActivity.this).execute(fromEmail,
fromPassword, toEmailList, emailSubject, emailBody);
But now i need to use Fragment and i get an error at this point: "MainActivity.this its not an enclosing class"
how can i solve this error?
P.S.: Sorry for my bad english :D
To get the current fragment that's active in your Android Activity class, you need to use the supportFragmentManager object. The supportFragmentManager has findFragmentById() and findFragmentByTag() methods that you can use to get a fragment instance.
A FragmentActivity is an instance of Activity (in Java style: FragmentActivity extends Activity ). So there is no point calling getActivity because a FragmentActivity is already an Acivity , so just calling/using this will work. Also there is no method called getActivity in the FragmentActivity class.
You can retrieve a NavController by using one of the following methods: Kotlin: Fragment. findNavController()
onAttach(Activity) called once the fragment is associated with its activity. onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With