Search code examples
c#visual-studioxamarin.android

logout button does not work despite of doing all I though was helpful


logout button does not work despite of doing all I though was helpful

Button logoutButton;

public override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    // Create your fragment here
}

public override View OnCreateView(LayoutInflater inflater, 
    ViewGroup container, Bundle savedInstanceState)
{
    // Use this to return your custom view for this Fragment
    View view = inflater.Inflate(Resource.Layout.account, container, false);

    logoutButton = (Button)view.FindViewById(Resource.Id.LogoutButton);
    logoutButton.Click += LogoutButton_Click;

    return view;
}

private void LogoutButton_Click(object sender, EventArgs e)
{
    StartActivity(typeof(LoginActivity));
}

Solution

  • This issue due to you lack of the new Intent() in the StartActivity like following code.

    public override View OnCreateView(LayoutInflater inflater, 
        ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        View view = inflater.Inflate(Resource.Layout.account, container, false);
    
        logoutButton = (Button)view.FindViewById(Resource.Id.LogoutButton);
        logoutButton.Click += LogoutButton_Click;
    
        return view;
    }
    
    private void LogoutButton_Click(object sender, System.EventArgs e)
    {
    
        StartActivity(new Intent(Application.Context, typeof(LoginActivity)  ));
    }
    

    Here is runnning GIF.

    enter image description here