How to handle button clicks using the XML onClick within Fragments

Pre-Honeycomb (Android 3), each Activity was registered to handle button clicks via the onClick tag in a Layout’s XML: android:onClick=”myClickMethod” Within that method you can use view.getId() and a switch statement to do the button logic. With the introduction of Honeycomb I’m breaking these Activities into Fragments which can be reused inside many different Activities. Most … Read more

ViewPager and fragments — what’s the right way to store fragment’s state?

Fragments seem to be very nice for separation of UI logic into some modules. But along with ViewPager its lifecycle is still misty to me. So Guru thoughts are badly needed! Edit See dumb solution below 😉 Scope Main activity has a ViewPager with fragments. Those fragments could implement a little bit different logic for … Read more

How do I get the currently displayed fragment?

I am playing with fragments in Android. I know I can change a fragment by using the following code: FragmentManager fragMgr = getSupportFragmentManager(); FragmentTransaction fragTrans = fragMgr.beginTransaction(); MyFragment myFragment = new MyFragment(); //my custom fragment fragTrans.replace(android.R.id.content, myFragment); fragTrans.addToBackStack(null); fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); fragTrans.commit(); My question is, in a Java file, how can I get the currently displayed Fragment … Read more

How to correctly save instance state of Fragments in back stack?

I have found many instances of a similar question on SO but no answer unfortunately meets my requirements. I have different layouts for portrait and landscape and I am using back stack, which both prevents me from using setRetainState() and tricks using configuration change routines. I show certain information to the user in TextViews, which … Read more

IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager

I’m getting user reports from my app in the market, delivering the following exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1109) at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:399) at android.app.Activity.onBackPressed(Activity.java:2066) at android.app.Activity.onKeyUp(Activity.java:2044) at android.view.KeyEvent.dispatch(KeyEvent.java:2529) at android.app.Activity.dispatchKeyEvent(Activity.java:2274) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1803) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1855) at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1277) at android.app.Activity.dispatchKeyEvent(Activity.java:2269) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1803) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112) at … Read more

Why fragments, and when to use fragments instead of activities?

In Android API 11+, Google has released a new class called Fragment. In the videos, Google suggests that whenever possible (link1, link2), we should use fragments instead of activities, but they didn’t explain exactly why. What’s the purpose of fragments and some possible uses of them (other than some UI examples that can be easily … Read more

Best practice for instantiating a new Android Fragment

I have seen two general practices to instantiate a new Fragment in an application: Fragment newFragment = new MyFragment(); and Fragment newFragment = MyFragment.newInstance(); The second option makes use of a static method newInstance() and generally contains the following method. public static Fragment newInstance() { MyFragment myFragment = new MyFragment(); return myFragment; } At first, … Read more

How to determine when Fragment becomes visible in ViewPager

Problem: Fragment onResume() in ViewPager is fired before the fragment becomes actually visible. For example, I have 2 fragments with ViewPager and FragmentPagerAdapter. The second fragment is only available for authorized users and I need to ask the user to log in when the fragment becomes visible (using an alert dialog). BUT the ViewPager creates … Read more