Replace Fragment inside a ViewPager

I’m trying to use Fragment with a ViewPager using the FragmentPagerAdapter. What I’m looking for to achieve is to replace a fragment, positioned on the first page of the ViewPager, with another one. The pager is composed of two pages. The first one is the FirstPagerFragment, the second one is the SecondPagerFragment. Clicking on a … Read more

Do fragments really need an empty constructor?

I have a Fragment with a constructor that takes multiple arguments. My app worked fine during development, but in production my users sometimes see this crash: 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 could make an empty constructor as this error … Read more

Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

I’m coming from iOS where it’s easy and you simply use a UIViewController. However, in Android things seem much more complicated, with certain UIComponents for specific API Levels. I’m reading BigNerdRanch for Android (the book is roughly 2 years old) and they suggest I use Activity to host my FragmentActivities. However, I thought Activity was … Read more

Android Fragment no view found for ID?

I have a fragment I am trying to add into a view. FragmentManager fragMgr=getSupportFragmentManager(); feed_parser_activity content = (feed_parser_activity)fragMgr .findFragmentById(R.id.feedContentContainer); FragmentTransaction xaction=fragMgr.beginTransaction(); if (content == null || content.isRemoving()) { content=new feed_parser_activity(item.getLink().toString()); xaction .add(R.id.feedContentContainer, content) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) .addToBackStack(null) .commit(); Log.e(“Abstract”, “DONE”); } When this code is executed I get the following error in debug.. java.lang.IllegalArgumentException: No view found … Read more

Update ViewPager dynamically?

I can’t update the content in ViewPager. What is the 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. Except I can’t change the content. So I found this: … Read more

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 … Read more

Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

I have an application with three tabs. Each tab has its own layout .xml file. The main.xml has its own map fragment. It’s the one that shows up when the application first launches. Everything works fine except for when I change between tabs. If I try to switch back to the map fragment tab, I … Read more

Difference between add(), replace(), and addToBackStack()

What is the main difference between calling these methods: fragmentTransaction.addToBackStack(name); fragmentTransaction.replace(containerViewId, fragment, tag); fragmentTransaction.add(containerViewId, fragment, tag); What does it mean to replace an already existing fragment, and adding a fragment to the activity state, and adding an activity to the back stack? Secondly, with findFragmentByTag(), does this search for tag added by the add()/replace() method … Read more