Hi everybody!

On Wednesday, May 29, 2013 9:35:49 PM UTC+2, Miha wrote:
>
> I'll post the whole mechanics tomorrow. 
>

So, to give some context: I'm using a ViewPager with 
FragmentStatePageAdapter, where I have three fragments. Two of those 
fragments (first and third), also nest another fragments inside. The first 
one has a two-pane layout, and the third one is sort of a file-browser 
where three types of fragments change (computer, shares, files).

We know that when we derive our activity, the BACK key is handled for us 
automatically by the FragmentActivity base class. This is of course not the 
case for nested fragments, where I need to "clear the backstack" 
appropriately. So for that, I've defined and implemented a simple interface:

public interface BackButtonPressListener { 
  boolean onBackButtonPressed(); 
}

I use this interface from the main Activity, which implements this 
interface. So when a Back key is pressed, I get the current fragmet from 
FragmentStatePageAdapter, and call onBackButtonPressed on that fragment. If 
this fragment is able to "pop" the back-stack, I leave it at that. If not, 
I let the base activity handle the key press.

I had the problem with FragmentStatePageAdapter, more so with its 
getItem(int) method. I was under the impression that this method would get 
the current fragment, but it is actually intended to **create** fragments; 
FragmentStatePageAdapter internally caches those instances for viewPager, 
so I went about implementing my own cache for that as well (since 
FragmentStatePageAdapter's cache is private). I overrode the method 
instantiateItem with such implementation:

public Object instantiateItem(ViewGroup container, int position) {
    Object item = super.instantiateItem(container, position);
    _items.put(position, item);
    return item;
}

where _items is a private member of type SparseArray<Fragment>. 

I added a simple method to get the reference to the current fragment being 
shown inside a ViewPager to my page adapter:
public Fragment getCreatedItem(int position) 
{
    Object item = _items.get(position, null);
    if (item != null)
    {
        return (Fragment) item;
    }
    Log.v(TAG, _items.toString());
    throw new RuntimeException("No matching item in an inner array or item 
is null..."); // shouldn't happen
}

The "onBackButtonPressed" in the "main" (top-level) fragments handler 
implementation is very simple as well:
@Override
public boolean onBackButtonPressed() {
    try {
        return FragmentManager fragmentManager = 
getChildFragmentManager().popBackStackImmediate();
} catch (Exception x) {
        Log.e(TAG, "Could not pop back stack!", x);
    }
    return false;
}

All of my fragments implement onSaveInstanceState to store state and so 
far, everything works as expected (screen orientation changes, resuming an 
app, back presses).

I hope this helps someone as well, given some misleading information out 
there.

Regards,
 Miha.

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to