Android replace the current fragment with another fragment

Then provided your button is showing and the click event is being fired you can call the following in your click event: final FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.details, new NewFragmentToReplace(), “NewFragmentTag”); ft.commit(); and if you want to go back to the DetailsFragment on clicking back ensure you add the above transaction to the back stack, … Read more

How can I execute something just once per application start?

SharedPreferences seems like ugly solution to me. It’s much more neat when you use application constructor for such purposes. All you need is to use your own Application class, not default one. public class MyApp extends Application { public MyApp() { // this method fires only once per application start. // getApplicationContext returns null here … Read more

Is there a way to get references for all currently active fragments in an Activity?

Looks like the API currently misses a method like “getFragments”. However using the event “onAttachFragment” of class Activity it should be quite easy to do what you want. I would do something like: List<WeakReference<Fragment>> fragList = new ArrayList<WeakReference<Fragment>>(); @Override public void onAttachFragment (Fragment fragment) { fragList.add(new WeakReference(fragment)); } public List<Fragment> getActiveFragments() { ArrayList<Fragment> ret = … Read more

How to use Holo.Light theme, and fall back to ‘Light’ on pre-honeycomb devices?

You have to create a custom theme and save it in some directories to finally set this theme as the default one for the app First, in values add a themes.xml like this: <?xml version=”1.0″ encoding=”utf-8″?> <resources> <style name=”MyAppTheme” parent=”@android:style/Theme.Light.NoTitleBar”> <!– Any customizations for your app running on pre-3.0 devices here –> </style> </resources> Then, … Read more

SearchView’s OnCloseListener doesn’t work

I also meet this problem, and I have no choice but give up “oncloselistener”. Instead, you can get your menuItem, then setOnActionExpandListener. Then override unimplents methods. @Override public boolean onMenuItemActionExpand(MenuItem item) { // TODO Auto-generated method stub Log.d(“*******”,”onMenuItemActionExpand”); return true; } @Override public boolean onMenuItemActionCollapse(MenuItem item) { //do what you want to when close the … Read more

Fragments onResume from back stack

For a lack of a better solution, I got this working for me: Assume I have 1 activity (MyActivity) and few fragments that replaces each other (only one is visible at a time). In MyActivity, add this listener: getSupportFragmentManager().addOnBackStackChangedListener(getListener()); (As you can see I’m using the compatibility package). getListener implementation: private OnBackStackChangedListener getListener() { OnBackStackChangedListener … Read more

Is it possible to display icons in a PopupMenu?

Contribution to the solution provided by Gaelan Bolger. Use this code if you get a “IllegalAccessException: access to field not allowed”. PopupMenu popup = new PopupMenu(mContext, view); try { Field[] fields = popup.getClass().getDeclaredFields(); for (Field field : fields) { if (“mPopup”.equals(field.getName())) { field.setAccessible(true); Object menuPopupHelper = field.get(popup); Class<?> classPopupHelper = Class.forName(menuPopupHelper .getClass().getName()); Method setForceIcons = … Read more

Changing overflow icon in the action bar

You can with a style, but you have to add it to the main Theme declaration. <resources> <!– Base application theme. –> <style name=”Your.Theme” parent=”@android:style/Theme.Holo”> <!– Pointer to Overflow style ***MUST*** go here or it will not work –> <item name=”android:actionOverflowButtonStyle”>@style/OverFlow</item> </style> <!– Styles –> <style name=”OverFlow” parent=”@android:style/Widget.Holo.ActionButton.Overflow”> <item name=”android:src”>@drawable/ic_action_overflow</item> </style> </resources> You also can … Read more