简体   繁体   中英

Call an Activity method from Fragment

I'm using ToDoActiviy.cs for user login, this class got this method:

[Java.Interop.Export()]

public async void LoginUser(View view) { if(await authenticate())..

This method is called from .axml file from Button widget android:onClick="LoginUser" I changed this for android:onClick="LoginUserClick" This last method create a dialog fragment for show different logins accounts.

Now from the Dialog Fragment(Is situated on another class) I want to hand the event for the button click on the dialog fragment and call this method from ToDoActivity.cs .

On dialog fragment class I hand the click event like this:

private void ButtonSignInFacebook_Click(object sender, EventArgs args){

//Here code for call to LoginUser method from 'ToDoActivity.cs' ToDoActiviy.cs act = new ToDoActivity(); act.LoginUser(); }

I need to pass a View but I tried a lot of things and any works..

Someone can help me?

Thanks in advance ;)

I would like to make a slight modification to @guido-gabriel 's answer.

In C# syntax, it will be

((ToDoActivity)Activity).yourPublicMethod();

Getter/Setter Methods in Java are mapped to Getter Setter properties in Xamarin.Android

Finally I fix it ! I had to change the parameters of the method and create it without parameters.. and now Is working. Both solutions are good:

((ToDoActivity)Activity).LoginUserFacebook(); //ToDoActivity act = new ToDoActivity(); //act.LoginUserFacebook();

You have to call the method from the activity. Have you tried?

((YourActivityClassName)getActivity()).yourPublicMethod();

IE

((ToDoActivity)getActivity()).yourPublicMethod();

Adapt and use the snipped below in your fragment

var casted = Activity as MyActivityName;

    if (casted != null) {
        casted.IWantToCallThisMethodFromMyFragment();
    }

This is not really a good practice to do. Why?

Doing this couples the Fragment tightly to this particular Activity type, meaning it will not be possible to reuse the Fragment elsewhere in the code.

Instead I suggest you rely on the Activity subscribing to an event or implementing some kind of callback method in order to do the desired action after login.

It could also seem like your Activity might be containing a lot of logic that could be split out into a shared library of some kind. Making it possible to reuse that code on another platform, for instance iOS in the future.

So since your are in charge of newing up the Fragment, I would do something like this instead:

public class LoginFragment : Fragment
{
    Action _onLoggedIn;

    public static void NewInstance(Action onLoggedIn)
    {
        var fragment = new LoginFragment();
        fragment._onLoggedIn = onLoggedIn;

        return fragment;
    }

    private void Login()
    {
        // login user
        // after loggedin
        _onLoggedIn?.Invoke();
    }
}

Then in your Activity:

private void LoginUser()
{
    // whatever
}

var loginFragment = LoginFragment.NewInstance(LoginUser);
// fragment transaction here...

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