Using PreferenceScreen with a transparent background

If you've ever tried to use a PreferenceScreen inside a PreferenceFragment which has a transparent background (or it's parent activity has transparent background), you should have noticed that, instead of replacing the fragment, PreferenceScreen adds the fragment which makes it overlap with the previous fragment (i.e top fragment's content is visible on top of the fragment just below it). So, here's a quick solution for this issue:


@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);
if (preference != null) {
if (preference instanceof PreferenceScreen) {
if (((PreferenceScreen) preference).getDialog() != null) {
((PreferenceScreen) preference)
.getDialog()
.getWindow()
.getDecorView()
.setBackgroundColor(Color.BLACK);
}
}
}
return false;
}

Go "Home" onNewIntent()!

After fiddling around for quite a long time, I've finally figured out how to pop the back stack or go to the top fragment or the top activity when the home button is pressed (provided that you are inside the app).


@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
!= Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) {
getFragmentManager().popBackStack(null,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}

Power function

In C/C++ (and also other languages), we have the built in power function to raise a number to a given power. What if we need to calculate ...