CodeJunkie

Trials and tribulations of an addicted Android developer.

Customizing EdgeEffect

If there’s one thing Android is good for, it’s being able to customize almost every aspect of your application, down to the nitty gritty. One area that some developers/designers may overlook when customizing is the EdgeEffect.

The EdgeEffect is an indicator to let the user know that he/she have reached the end of the scrolling container. You’ll also know it as the blue overscroll glow…

Managing Fragment States Manually

If you find yourself in a bit of a sticky situation where you need to save Fragment state manually, thankfully Android has a simply way of accomplishing this.

Step 1 - Get the Fragment

Obtain the target Fragment. Assuming you’ve already attached it to your Activity, use FragmentManager.findFragmentById() or FragmentManager.findFragmentByTag(). For example…

Fragment myFragment = getFragmentManager.findFragmentById(R.id.content);

Step 2 - Save with saveFragmentInstanceState

Call saveFragmentInstanceState of your FragmentManager and pass the target Fragment through. For example…

Fragment.SavedState myFragmentState = getFragmentManager().saveFragmentInstanceState(myFragment);

The saveFragmentInstanceState method will return a Fragment.SavedState object which you will hold on to until you need to restore your Fragments.

Step 3 - Restore with setInitialSavedState

When you’re ready to restore your Fragment, call setInitialSavedState(), passing through the Fragment.SavedState object you held on to. For example…

Fragment myFragment = new MyFragment();
myFragment.setInitialSavedState(myFragmentState);

// Attach Fragments....

You’ll also want to implement the onSavedInstanceState() method in your target Fragment. This method will be called when saving state. Put anything you want into the provided Bundle that needs to be persisted (like counters and such) and restore in onCreateView or onCreate.

Notifying ViewPager Fragments

As the title suggests, this post is about notifying Fragments in a ViewPager. Even though PagerAdapter has a notifyDataSetChanged() method, this method is for a different purpose. There are undoubtly more than one way to solve this problem, but I found this method to be very simple to implement and easy to understand.

Using Custom ActionBar Title Views

The Problem

The default ActionBar is good for most use cases, however some applications may require further customization to establish a brand. Customizing the ActionBar title may seem quite trivial, however, there are a couple of things that you can do to create an even more polished experience.

As you may have guessed already, setting a custom View is the way to go…

1
2
3
4
5
6
7
8
9
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActionBar ab = getActionBar();
    ab.setDisplayShowTitleEnabled(false);
    ab.setDisplayShowCustomEnabled(true);
    View customTitle = getLayoutInflater().inflate(R.layout.custom_title, null);
    ab.setCustomView(mTitleView);
}

The above code will absolutely work with zero problems…

Retaining Fragments in FragmentPagerAdapter

The general pattern for using a FragmentPagerAdapter looks something like this…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        return ArrayListFragment.newInstance(position);
    }
}

The above code works perfectly, however if getItem(int) was to be called by anything other than FragmentPagerAdapter, things may get weird.

The getItem(int) method gets called by instantiateItem(ViewGroup, int) if the Fragment at the given position doesn’t exist already in the FragmentManager. This explains why getItem(int) returns a new instance every time it’s called in the snippet above.