Tag: android-fragments

Context in a PreferenceFragment

taken from http://developer.android.com/guide/topics/ui/settings.html: Note: A PreferenceFragment doesn’t have a its own Context object. If you need a Context object, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() […]

JUnit testing for Android app with fragments

My Android App was built on Single Activity, multiple fragments based model. I need to do unit testing for the app. I could write unit testcases for app which contains all activities using ActivityInstrumentationTestCase2 JUnit but not for app which contains fragments. Please suggest the way to write JUnit testcases for fragments. Thank you

Passing interface to Fragment

Let’s consider a case where I have Fragment A and Fragment B. B declares: public interface MyInterface { public void onTrigger(int position); } A implements this interface. When pushing Fragment B into stack, how should I pass reference of Fragment A for it in Bundle so A can get the onTrigger callback when needed. My […]

Fragment must be a public static class to be properly recreated from instance state

After updating to the latest support repository, compile ‘com.android.support:appcompat-v7:24.2.0’ compile ‘com.android.support:design:24.2.0’ compile ‘com.android.support:percent:24.2.0’ compile ‘com.android.support:recyclerview-v7:24.2.0′ I’m getting the weird exception. java.lang.IllegalStateException: Fragment null must be a public static class to be properly recreated from instance state. at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:435) at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:414) at android.support.v4.app.DialogFragment.show(DialogFragment.java:154) at com.androidapp.base.BaseActivity.showDialogFragment(BaseActivity.java:78) at com.androidapp.MainActivity.showNewDialog(MainActivity.java:304) at com.androidapp.MainActivity$6.onClick(MainActivity.java:228) In my BaseActivity class, I’ve created a […]

getParentFragment API 16

We all know getParentFragment of Fragment is introduced in API 17. So what if we want to get parent fragment in API 16 and below (Considering that I use native Fragment with support FragmentStatePagerAdapter and have no problem with nested fragments) Is there any better way than mine? In parent: public class ParentFragment extends Fragment […]

FragmentPagerAdapter – how to detect a swipe or a tab click when user goes to a new tab?

I have a MainActivity that has three fragments in a FragmentPagerAdapter as below. How can I find out when an user goes from 1st fragment to second or from second to third, either with swiping or with a click on the tab? I saw that the getItem() method is not called always as I have […]

How to switch between fragments during onclick?

I have a project I’m trying to do. I ran into a little issue and I’m not sure how to get around it. Below is an image of the application so far. What I would like it to do is when the user onclicks one of the list items, the part which says “Hello! It’s […]

Passing an Object to Fragment or DialogFragment upon Instantiation

I’m trying to work out the correct way to pass in an Object to a Fragment or DialogFragment without breaking the ’empty constructor’ rule. For example I have created a custom View and for each one I instantiate I want to associate a DiaglogFragment. This DialogFragment will be used to display controls with which the […]

how load fragment in ViewPager only when its selected

im using 3 Fragments inside a Viewpager, the problem its im loading big data in asytask and loaders, in devices like HTC one works well, however in low end devices take a lot of time. THis is because mainly when i implement the pagerAdapter i put the Fragments inside an arrayList, this force the fragments […]

implement AsyncTask in Fragment android

I’ve an activity which output json data from as a list. But now I want to implement it in a fragment. In this fragment I want to view it as gridview. And both files works fine. but when I tried to implement AsyncTask I gets several redflags as unreachable code. Can someone help me with […]

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