Tag: android-fragments

Call an activity method from a fragment

Trying to call a method in my activity from a fragment. I want the fragment to give the method data and to get the data when the method return. I want to achieve similar to call on a static method, but without the use of static because it create problems in the activity. New to […]

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 […]

Understanding Fragment's setRetainInstance(boolean)

Starting with the documentation: public void setRetainInstance (boolean retain) Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated: onDestroy() will not be […]

findViewById in Fragment

I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment. However, the findViewById method only works if I extend an Activity class. Is there anyway of which I can use it in Fragment as well? public class TestClass […]

Update ViewPager dynamically?

I can’t update the content in ViewPager. A question: What is the relationship and correct usage of methods instantiateItem() and getItem() in FragmentPagerAdapter class? I was using only getItem() to instantiate and return my fragments: @Override public Fragment getItem(int position) { return new MyFragment(context, paramters); } This worked well (but as said I can’t change […]

Do fragments really need an empty constructor?

I have a fragment with a Constructor with multiple arguments, everything worked fine during testphase but now after about 300 users downloaded the app, i have ONE occurence of that exception: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public I mean i could […]

Android Fragment onAttach() deprecated

I have updated my app to use the latest support library (version 23.0.0), I’ve found out that they deprecated the onAttach() function of the Fragment class. Instead of: onAttach (Activity activity) It’s now: onAttach (Context context) As my app uses the activity passed before deprecation, a possible solution i think is: @Override public void onAttach(Context […]

Once for all, 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 […]

onActivityResult is not being called in Fragment

The activity hosting this fragment has its onActivityResult called when the camera activity returns. My fragment starts an activity for a result with the intent sent for the camera to take a picture. The picture application loads fine, takes a picture, and returns. The onActivityResult however is never hit. I’ve set breakpoints, but nothing is […]

get currently displayed fragment

I am playing with Fragment in Android. I know I can change 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 instance ?

Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.