Question

I'm using Xamarin. So it's c# code.

I have a fragment which basically fill vehicle information from a web services and save it to a database. The web service asynch function is call in the OnActivityCreated() and the filling information is done in the corresponding completedevent.

This fragment is always close in the end of the process using FragmentManager.PopBackStack();

When this fragment close the person can open again this fragment to save another vehicle.

Each fragment backpress is handle here :

public override void OnBackPressed()
    {
        FragmentManager.PopBackStackImmediate();
        if (FragmentManager.BackStackEntryCount == 0)
        {
            Finish();
        }
        else
        {
            String tagFrag = FragmentManager.GetBackStackEntryAt(FragmentManager.BackStackEntryCount - 1).Name;
            Fragment previousFrag = FragmentManager.FindFragmentByTag(tagFrag);
            AndroidCommon.ShowFragment(this.FragmentManager, Resource.Id.content_frame, previousFrag, tagFrag);
        }
    }

And each fragment SHOW is handle by this :

 public static void ShowFragment(FragmentManager fm, int contentId, Fragment fragToShow, string fragTag)
    {
        String previousFragTag = "";
        Fragment previousFrag = null;
        try
        {
            if (fm.BackStackEntryCount > 0)
            {
                previousFragTag = fm.GetBackStackEntryAt(fm.BackStackEntryCount - 1).Name;
                previousFrag = fm.FindFragmentByTag(previousFragTag);

            }
            if (fragTag != previousFragTag)
            {
                FragmentTransaction fragmentTx;
                fragmentTx = fm.BeginTransaction();
                fragmentTx.AddToBackStack(fragTag);
                fragmentTx.Replace(contentId, fragToShow, fragTag);
                fragmentTx.SetTransition(FragmentTransit.FragmentFade);
                fragmentTx.Commit();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

The first time that fragment is shown everything works fined, the vehicle is filled and saved, the popbackstack is called and bring the previous fragment. If the person open again the same fragment, i can see that onAttach and OnCreateView function have an activity.

However, when the completedevent from the async method trigger (is completed) and i try to peak the Activity in this method the Activity is Null.

In resume :

  • Open fragment principal (with replace) ->
  • Open addvehicle fragment (with replace)->
  • Close the add vehicle fragment with popbackstack ->
  • fragment principal come back ->
  • Open addvehicle fragment again ->
  • call event in OnActivityCreated->
  • OnCompleted event Activity is null

Note:

  1. My service web is in a static class to help me share in IOS and Android project.
  2. It works flawless on the first time instanced
Was it helpful?

Solution

I found the problem, it's seem that the static global variable which was my webservice loosed it's reference at some point. I suppose that when the completed event occur the global variable seem to point no where and the activity can no longer be referenced.

Solution : Instance it on the fragment "each time" when needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top