Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current activity from fragment

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

like image 500
nelletto Avatar asked Jun 17 '16 22:06

nelletto


People also ask

How do you find current active fragments?

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.

How do you get FragmentActivity from activity?

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.

How do I get NavController in fragment?

You can retrieve a NavController by using one of the following methods: Kotlin: Fragment. findNavController()

What is onAttach in fragment?

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.


2 Answers

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

like image 193
Lavanya Pant Avatar answered Sep 18 '22 17:09

Lavanya Pant


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();
like image 24
BOUTERBIAT Oualid Avatar answered Sep 18 '22 17:09

BOUTERBIAT Oualid