DMCApps / NavigationFragment

The purpose of this manager is to work with the ViewPager, Tabs and Navigation drawer to handle a single stack flow of fragments on the screen. It makes use of a main Fragment as a container and presents and hides fragments within it as children.
MIT License
28 stars 8 forks source link

fragment was instantiated too soon in the transactions #10

Closed jjhesk closed 8 years ago

jjhesk commented 8 years ago

I have noticed that in some situation when i start using navigationfragment it will trigger onMeasure too soon that caused crash from the initiation. However this bug is not constantly happening and that will occurs randomly. do you think it has anything to relate to this? I am trying to reproduce it and capture more information for this.

I captured onMeasure() illegalstate exception like so:

baconlmy48yhesk02262016112421

baconlmy48yhesk02262016112445

Currently there is an Fragment Adapter using like so:


package com.ogaclejapan.smarttablayout.utils.v4;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.util.SparseArrayCompat;
import android.view.ViewGroup;

import java.lang.ref.WeakReference;

public class FragmentPagerItemAdapter extends FragmentPagerAdapter {

  private final FragmentPagerItems pages;
  private final SparseArrayCompat<WeakReference<Fragment>> holder;

  public FragmentPagerItemAdapter(FragmentManager fm, FragmentPagerItems pages) {
    super(fm);
    this.pages = pages;
    this.holder = new SparseArrayCompat<>(pages.size());
  }

  @Override
  public int getCount() {
    return pages.size();
  }

  @Override
  public Fragment getItem(int position) {
    return getPagerItem(position).instantiate(pages.getContext(), position);
  }

  @Override
  public Object instantiateItem(ViewGroup container, int position) {
    Object item = super.instantiateItem(container, position);
    if (item instanceof Fragment) {
      holder.put(position, new WeakReference<Fragment>((Fragment) item));
    }
    return item;
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    holder.remove(position);
    super.destroyItem(container, position, object);
  }

  @Override
  public CharSequence getPageTitle(int position) {
    return getPagerItem(position).getTitle();
  }

  @Override
  public float getPageWidth(int position) {
    return super.getPageWidth(position);
  }

  public Fragment getPage(int position) {
    final WeakReference<Fragment> weakRefItem = holder.get(position);
    return (weakRefItem != null) ? weakRefItem.get() : null;
  }

  protected FragmentPagerItem getPagerItem(int position) {
    return pages.get(position);
  }

}
DMCApps commented 8 years ago

I'm not sure what this could be. I have been using the library in my own application and I have not received this issue. Do you have any views in your application that you override onMeasure()? Maybe something like this is occurring? https://github.com/MikeOrtiz/TouchImageView/issues/47

Base on the commit linked in that repo, they added in a default setMeasuredDimension when the drawable they attempted to use was null.

jjhesk commented 8 years ago

Even i took out the swiperefreshlayout to nothing and it occurs on another level of onMeasure.

@mikeortiz was mentioned the check null method to void the crash which is this

 if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0) {
setMeasuredDimension(0, 0);
return;
 }

or

super.onMeasure(widthMeasureSpec, heightMeasureSpec); but it doesnt seem to work in my case.

DMCApps commented 8 years ago

Can you give me where you are creating the SingleStackNavigationManagerFragment? Do you have a full working example of when the crashes happen so I can investigate further?

jjhesk commented 8 years ago

Here it is.. issue_onMeasure branch

jjhesk commented 8 years ago

i strongly believe that it has something to do with the weakreference because I have seen this cause even coming out when i took out the navigationfragment while it was trying to access the objects from the garbage. .. i hope that this library could support smarttablayout library because it can do much more in plenty different varieties.

jjhesk commented 8 years ago

i discovered the issue with the inner library.

DMCApps commented 8 years ago

Sorry can you elaborate on what the issue was? When you say inner library is it my library? Can you please do a pull request so I can fix it for others? If it wasn't with the library then this will help others fix the same issue in their project. If it is with the issue I would love to incorporate any work that you put into this library to ensure the best product for everyone!

I have re-opened the issue until I know for certain it is not the library code or that it is changed in the library.

Please note all development is taking place on the develop branch. I'd like to keep the master clean as the current live code so that I can manage any hot fixes from there (such as this fix you are talking about and #11 )

jjhesk commented 8 years ago

wait. this issue is still incur in the back button on press. When the navigationfragment popback to the previous fragment. This onMeasure() bug just pop out. @DMCApps sorry. this bug is not fixed yet. I had a setup with the swiperefresh component bind in the xml on the previous fragment but it happened to crash when it pops back.