0

I have an ActionBarActivity (which, by the way, extends android.support.v4.app.FragmentActivity) and I want to add a fragment to topmost. Here is my code:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
NotificationsFragment fragment = new NotificationsFragment();
ft.add(R.id.pager, fragment).commit();

Where:

  • NotificationsFragment extends android.support.v4.app.Fragment
  • R.id.pager is the id of the root view (which is a android.support.v4.view.ViewPager)

When the code above runs, nothing happens. No exceptions, crashes or any visual changes. Just nothing. I've seen FragmentTransaction not doing anything and there was a suggestion there telling to use replace instead of add, and when I tried that, pager's fragment (remember, my root view is a pager) that was being displayed disappeared.

I have no idea what's going on, and I'm also new to Android. What am I doing wrong?

Note: My minimum API target in ICS (15), so I don't need to support any older versions, so any solution involving newer APIs are preferred.

Community
  • 1
  • 1
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • Are you determined to make the code work in its current state? Because to me it's a bit of an unconventional usage of a `ViewPager`, which is usually filled with fragments through an adapter like [here](https://github.com/codepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter) for example. On the other hand, ViewPager is still a `ViewGroup`, so mb you *can* make is work eventually... – AndroidEx Apr 09 '15 at 23:31
  • @Android777 i actually folowed android studio's default template and it created this. – Can Poyrazoğlu Apr 09 '15 at 23:39
  • That's strange, but the first answer is the solution. – AndroidEx Apr 09 '15 at 23:41
  • You must use some sort of `PagerAdapter`. In the same way as views such as `ListView` need an `Adapter` of some sort to generate each list item, a `ViewPager` requires an `Adapter` to create each item that makes up a 'page'. – Squonk Apr 09 '15 at 23:45
  • @Squonk but I don't want this fragment to get into a page. It should be displayed on top of everything. – Can Poyrazoğlu Apr 10 '15 at 12:20
  • @CanPoyrazoğlu do you mean you don't in fact need viewpager-like behaviour? In this case you can put a basic frame layout in your activity xml layout, and use is as a container `ft.add(R.id.[frame_layout_id], fragment).commit();` – AndroidEx Apr 10 '15 at 19:12
  • @Android777 I've actually just found a solution. First, yes I do need a viewpager-like behavior. The solution was much more simple. I've added a frame layout as the root view, and added the pager as it's child. Then I've added my fragment to the root view, instead of pager, which simply wasn't playing nice with custom fragments. It works. – Can Poyrazoğlu Apr 10 '15 at 19:48

2 Answers2

0

I have used ViewPager in my application and I used the FragmentStatePagerAdapter. It will take care of the fragment management for you.

You can read more about ViewPager here.

and FragmentStatePagerAdapter here.

Christian Abella
  • 5,747
  • 2
  • 30
  • 42
0

I've found the solution myself. I've added a frame layout as the root view, and added the pager as it's child. Then I've added my fragment to the root view, instead of pager. It works, apparently the view pager simply wasn't playing nice with custom fragments added to itself.

Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389