Fragment.isAdded() 在 support-v4:23.4.0 中的行为与之前的版本不同

Fragment.isAdded() behaves differently in support-v4:23.4.0 than in prior versions

当我将我的 support-v4 版本从 23.1.1 升级到 23.4.0 时,我注意到了这个问题:基本上,isAdded() 在过去 return 的情况下总是 returning false ] 是的。

我有一个 activity (FragmentActivity),它有一个包含片段的 ViewPager。每个片段在开始时都会在 onCreate() 中启动一个异步任务来下载一些图像;为了提高效率,在回调中,我正在检查 isAdded() 以确保在继续处理之前附加了 Fragment。

如果我包含 support-v4 库的版本 23.1.1,我的代码将按预期工作。但是,当我更新到 23.4.0 时,isAdded() 似乎几乎总是 return false,这甚至不允许当前片段完成处理异步结果。

注意:我是否翻阅相册并不重要——每次调用 isAdded() 似乎 return 错误。

相关代码如下(注意:本例简化了部分代码):

 // note FetchableListener implements onFetchableUpdate()
 public class CameraAlbumItemFragment
   implements Fetchable.FetchableListener
 {
    private static final String CAMERA_ALBUM_ITEM_FRAGMENT_CAMERA_KEY = "camera" ;

    // Member Variables
    //
    @Nullable private Camera m_camera ;
    @Nullable private ArrayList<CameraViewImageDownloadResult> m_imageDownloads;

    public static CameraAlbumItemFragment newInstance ( @NotNull Camera camera )
    {
       final CameraAlbumItemFragment fragment = new CameraAlbumItemFragment();
       fragment.setRetainInstance( true );

       final Bundle bundle = new Bundle( 1 );
       bundle.putParcelable( CAMERA_ALBUM_ITEM_FRAGMENT_CAMERA_KEY, camera );

       fragment.setArguments( bundle );

       return fragment;
    }

    @Override
    public void onCreate ( @Nullable Bundle savedInstanceState )
    {
       super.onCreate( savedInstanceState );

       m_camera = getArguments().getParcelable( CAMERA_ALBUM_ITEM_FRAGMENT_CAMERA_KEY );

      // If the images have not been downloaded, then start background
      // tasks to retrieve them. Not likely, but make sure our camera is not null
      //
      if ( m_camera != null && m_imageDownloads == null )
      {
          // This will start an async task that will call onFetchableUpdate() when it receives a response from the server
          m_camera.updateNonCurrentViews( getActivity(), this );
      }
   }

   /** The Fragment's UI */
   @Override
   public View onCreateView ( @NotNull LayoutInflater inflater,
                              @Nullable ViewGroup container,
                              @Nullable Bundle savedInstanceState )
   {
      final View view = inflater.inflate( R.layout.camera_album_item, container, false );

      // Set image if already downloaded
      //
      // Set the on click listener for the cycle image button
      // This has to be done here instead of using the android:onClick attribute in the layout
      // file because this is a fragment, not an activity
      //
      final ImageView cameraImageView = (ImageView) view.findViewById( R.id.camera_image_view );

      return view;
   }

   /**
    * Add an image to the list of downloaded images. Display or hide the cycle images button based on
    * the number of retrieved images
    *
    * @param bitmap An image retrieved by a background process
    */
   public void addImage ( @Nullable final Bitmap bitmap, @NotNull final String viewName )
   {
      assert m_imageDownloads != null;
      m_imageDownloads.add( new CameraViewImageDownloadResult( bitmap, viewName ) );
   }

   @Override
   public void onFetchableUpdate ( @NotNull Fetchable fetchable, @Nullable Object data )
   {
       //*********************************************************
       // NOTE: It is the call here to isAdded() that is returning false nearly 
       //       every time in support-v4:23.4.0 but not in 23.1.1
       //**********************************************************
      if ( fetchable == m_camera && isAdded() )
      {
         final List<CameraView> cameraViews = m_camera.getViews();
         m_imageDownloads = new ArrayList<>( cameraViews.size() );

         // Download camera images
         for ( CameraView cameraView : cameraViews )
         {
            if ( cameraView.isCurrent() )
            {
               final String imageURL = cameraView.getCameraURL();

               if ( imageURL != null )
               {
                  new GetCameraImageAsyncTask( this, cameraView.getName() ).execute( imageURL );
               }
               else
               {
                  Log.e( LOG_TAG, "No valid image URL for " + cameraView.getName() ) ;
                  addImage( null, cameraView.getName() );
               }
            }
            else
            {
               addImage( null, cameraView.getName() );
            }
         }

         // We don't need to maintain the observer reference anymore
         m_camera.removeListener( this );
      }
   }

   /**
    * Get the image view for displaying a camera view
    *
    * @return The camera image view
    */
   @Nullable
   private ImageView getCameraImageView ()
   {
      final View v = getView();
      if ( v != null )
      {
         return (ImageView)v.findViewById( R.id.camera_image_view );
      }
      else
      {
         return null;
      }
   }
}

及其包含 ViewPager

的 Activity (FragmentActivity)
 public class CameraAlbumActivity
    extends FragmentActivity
 {
    // Intent Data Key
    //
    public final static String CAMERA_ALBUM_SELECTED_ID_KEY = "selectedId" ;

     private static final String LOG_TAG = "CameraAlbumActivity" ;

    @Override
    protected void onCreate ( @Nullable Bundle savedInstanceState )
    {
       super.onCreate( savedInstanceState );

       final Intent intent = getIntent();

       final Object sharedData = SharedDataWrapper.getInstance().getData();

        CameraCollection cameraCollection ;

        if ( sharedData != null && sharedData instanceof CameraCollection )
        {
            cameraCollection = ( CameraCollection ) sharedData;
        }
        else
        {
            // just create an empty collection
            cameraCollection = new CameraCollection() ;
        }

        // Load view
        setContentView( R.layout.album );

        // Get references to buttons
        //
        m_previousButton = (ImageView)findViewById( R.id.album_previous_btn );
        m_nextButton = (ImageView)findViewById( R.id.album_next_btn );

        // Configure view pager
        //
        m_viewPager = (ViewPager)findViewById( R.id.album_view_pager );

       final CameraAlbumPagerAdapter adapter = new CameraAlbumPagerAdapter( getSupportFragmentManager(), cameraCollection );
       m_viewPager.setAdapter( adapter );
       m_viewPager.addOnPageChangeListener( new OnCyclingContentAlbumViewScrollListener( this, adapter ) );

       // Set the selected item
       int selectedId = intent.getIntExtra( CAMERA_ALBUM_SELECTED_ID_KEY, -1 );
       if( selectedId == -1 )
       {
          return;
       }

       List<Camera> models = cameraCollection.getAllModels();

       for ( int i = 0 ; i < models.size() ; i++ )
       {
           Camera camera = models.get( i );
           if ( selectedId == camera.getId() )
           {
               m_viewPager.setCurrentItem( i, false );
               break;
           }
       }
    }

     /**
      * OnPageChangeListeners should be removed to prevent memory leaks
      */
     @Override
     public void onDestroy()
     {
         m_viewPager.clearOnPageChangeListeners() ;

         super.onDestroy() ;
     }

     /**
      * Scroll one item to the right, if possible
      *
      * @param v the view triggering the event
      */
     public void scrollToNext ( @SuppressWarnings("UnusedParameters") View v )
     {
        int currentIndex = m_viewPager.getCurrentItem();

        if( currentIndex < m_viewPager.getAdapter().getCount() - 1 )
        {
           m_viewPager.setCurrentItem( currentIndex + 1, true );
        }
     }

     /**
      * Scroll one item to the left, if possible
      *
      * @param v the view triggering the event
      */
     public void scrollToPrevious ( @SuppressWarnings("UnusedParameters") View v )
     {
        int currentIndex = m_viewPager.getCurrentItem();

        if( currentIndex > 0 )
        {
           m_viewPager.setCurrentItem( currentIndex - 1, true );
        }
     }

     /**
      * Set the visibility of the previous and next buttons based on view pager contents and position
      */
     public void setPreviousAndNextButtonVisibility ()
     {
        final int position = m_viewPager.getCurrentItem();

        m_previousButton.setVisibility( position == 0 ? View.INVISIBLE : View.VISIBLE );
        m_nextButton.setVisibility( position < m_viewPager.getAdapter().getCount() - 1 ? View.VISIBLE : View.INVISIBLE );
     }

     /**
      * @return The item fragment which is currently displayed
      */
     @Nullable
     public Fragment getCurrentItemFragment ()
     {
        int currentItem = m_viewPager.getCurrentItem();
        ModelCollectionAlbumPagerAdapter adapter = (ModelCollectionAlbumPagerAdapter)m_viewPager.getAdapter();
        return adapter.getRegisteredFragment( currentItem );
     }

 }

我不确定是这个版本的支持库有问题(希望如此)还是我的代码中有什么不正确的地方最终在这个最新版本中浮出水面。正如我提到的,如果我只是在我的 gradle 文件中交换版本,上面的代码在 v 23.1.1 中按预期工作,但是当我更改为 23.4.0 时,它失败了。

想法?建议?

谢谢!

经过进一步调查,对支持库的更新揭示了现有代码中的缺陷。请注意,异步任务的启动始于 onCreate()。如果异步任务在 onCreateView() 完成之前完成,则当前 Fragment.isAdded() 调用将 return false。

无论出于何种原因,使用较旧的支持库,都没有发生这种情况(或者即使发生了,我也很少没有观察到)。新支持库的更新相当一致地触发了这种情况。

解决方法是将异步任务的开始移动到 onActivityCreated() 中,当然,它会在添加片段后调用。

在 isAdded() 之前调用它解决了我的问题。

getSupportFragmentManager().executePendingTransactions();