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

时间:2016-05-23 23:45:14

标签: android android-fragments android-fragmentactivity support-v4

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

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

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

注意:如果我翻阅相册并不重要 - 每次调用isAdded()似乎都会返回false。

下面的相关代码(注意:此示例简化了一些代码):

 // 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的活动(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文件中交换版本,上面的代码在第23.1.1节中按预期工作,但是当我更改为23.4.0时,它会失败。

思考?建议?

谢谢!

2 个答案:

答案 0 :(得分:1)

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

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

修复是将异步任务的开始移动到onActivityCreated(),当然,在添加了Fragment之后调用它。

答案 1 :(得分:0)

在isAdded()之前调用此方法为我解决了这个问题。

getSupportFragmentManager().executePendingTransactions();