Android viewpager in fragment - android

Dear ladies and gents,
First of all, please do not mark my question down. If you think that my question is too stupid please let me know and i will edit it or remove.
So my actual question is that I have an activity (extends Activity). In that activity's layout I created FrameLayout where I then attach different four fragments(so each of them extends Fragment. So in one of that fragments I would like to implements swipeable tabs(see screenshot).
I know that it is usually done by implementing viewPager and FragmentPagerAdapter, but I can not do this as if I'm calling getSupportFragmentManager in my fragment it gives my an error. If i am using getActivity().getFragmentManager() it gives me error that android.support.v4.app.fragmentmanager cannot be applied to android.app.fragmentmanager. I have to use android.support.v4.app.fragment in my fragment, because otherwise I will not be able to implement my activity's view(described at the beggining).
Any ideas or suggestions would be very appreciated.screenshot

Make sure you are using Fragment class that you extends your fragments comes from support library. You also need to use FragmentActivity to call method getSupportFragmentManager();
On the other hand, viewpager which is in your fragment need to implemented as usual that you can find on internet except getChildSupportFragmentManager();
This one called "nested fragments".
PS: I am not sure but you can also use AppCompatActivity instead of FragmentActivity. FragmentActivity and ActionBarActivity must be deprecated.
Good luck

You can use getChildFragmentManager() when using Fragments inside other Fragments.

New version of Support Library v4 (Android 4.2) resolve this problem. For do this, simply do constructor of your custom FragmentPagerAdapter like this:
public CustomFragmentPagerAdapter(android.support.v4.app.Fragment fragment)
{
super(fragment.getChildFragmentManager());
// write your code here
}
This work because new Android version approve using nested Fragments

Related

Fragment Transaction not working when using AppCompatActivity or FragmentActivity

I have a typical application. An activity which has a FrameLayout and in this layout I want to switch between fragments. This is typically and easily done with:
getFragmentManager().beginTransaction()
.replace(R.id.ac_container, new FrOverview())
.addToBackStack(null)
.commit();
The problem is, that even if I use .addToBackStack(null) (And I know it's been added 'cause the stack count increases) when I press back I exit the application. I have been trying a lot of different code stuff and checked most threads here on Stackoverflow but I can't get it to work with code (method calls etc.).
But! I can get it to work, by changing the extended class of my activity class. If my class extends Activity, it works fine. But if I use AppCompatActivity (which in turn extends FragmentActivity) then it has the bad behaviour as explained earlier.
Feels like this has to be an error on Androids part, I am not doing anything wrong to my knowledge.
Does anyone have any suggestions on how to solve this? i.e. get the back functionality and keep the ActionBar!
AppCompatActivity uses the SupportFragmentManager, you need
to switch to SupportFragment and SupportFragmentManager

how to start an activity inside fragment viewpager

I'm creating a swipeable Sherlock tab. I used this tutorial to do it:
Android ActionBarSherlock ViewPager Tabs Tutorial
However when I want to start an activity from inside of a fragment (for example FragmentTab1) I need context to create intent and start activity.
I'm in the Fragment class and don't have access to context!
I can't use getActivity() too, because FragmentTab1 is created inside of getItem() function in FragmentPagerAdapter class.
What should I do?!
Sherlock Fragments works with getSherlockActivity() instead of getActivity()
You should try using
getSherlockActivity().startActivity(...)
I'm in the Fragment class and don't have access to context!
Yes, you do. Call getActivity() to return the Activity that is hosting this fragment. Activity inherits from Context.
I can't use getActivity() too, because FragmentTab1 is created inside of getItem() function in FragmentPagerAdapter class.
So? That does not somehow magically cause the getActivity() method to vanish.
For some reason, you are using ActionBarSherlock, which has been deprecated for about 20 months by its author. If your fragment is a SherlockFragment, you will want to call getSherlockActivity() instead.

Trying to Create a Button to Open a Fragment (Android)

So I basically have a button in 'DemosFragment' and when I click it, I want it to open another fragment (SettingsFragment), I understand now that I need an activity to fix this issue, as the button currently has an onClick method using intent/startActivity, so how would I go about creating an activity that just holds my fragment? I know that may sound weird they way I wrote it, I just started Android development, but basically I have a fragment and because I want a fragment to have a button to open another fragment, I figure I need an activity for the fragment I am trying to open, so how do I create that activity and what do I need to put in it? Thanks.
You need an activity with the following code:
public class ShowFragmentActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_fragment);
}
}
You also have to create a layout xml file called activity_show_fragment.xml in your res/layout folder:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.yourFragmentsClassName"
android:id="#+id/fragment_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
This should work for just displaying your fragment.
To launch the activity, paste this code in your button's onClick method:
Intent i = new Intent(this, ShowFragmentActivity.class);
startActivity(i);
It's always a good decision to look at the official docs: http://developer.android.com/reference/android/app/Fragment.html.
Hope that helps!
Wow! Your question requires a long answer, however is a good practice (and madatory too) that Fragments cannot communicates between each others, but they can be hosted by an Activity; in that case an Activity can manage the communication flow between them (fragments) and can be developed in several ways, Bundle, Intent and the Handler. Have a look to the ufficial Android documentation here:
http://developer.android.com/training/basics/fragments/index.html
The android docs section on building a flexible UI is a good example of how to start/load a Fragment from an Activity. In the example you will see that a FrameLayout in the Activity XML is used a the fragment container. This will be the View in which all of your fragments are displayed.
When you load your fragment with a FragmentTransaction the contents of your fragments layout will be displayed in the container View. In the above referenced example this takes place with SupportFragmentManager a class included with the android support library, for facilitating fragment transactions in earlier version of the operating system. SupportFramgnetManager requires that you extend FramentActivity and not just Activity. If you're not worried about backwards compatibility and are extending activity, not fragment activity, you can simply use getFragmentManager() instead.
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
After the initial add transaction you can switch between fragments using the replace method for your fragment transaction. Replace does exactly what it sounds like, it swaps one fragment for another. To accomplish this from within your firstframgnet use
SecondFragment secondFragment = new SecondFragment();
getActivity().getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, secondFragment).commit();
Notice that from within the fragment I used getActivity(). This allows you to reference the context of the host activity to access the fragment manager. When you are within the activity you do not need to use getactivity because the fragment manager is already accessible from that context.

Communication fragments/activities

In my existing app I am porting two activities to fragments. The case is the classic dual panel mode with a list on the left and the content on the right.
The doc says that I should avoid to manipulate fragments within fragments, passing instead through the host activity. Said that I am using callbacks to the activity.
The first doubt (maybe banal) I have is:
How to avoid to duplicate the same code in the activity that hosts the
2 fragments and into the activity that wraps the fragment when not in
dual mode?
I'll try to explain. So I have:
ListFragment and ListFragmentActivity
ContentFragment and ContentFragmentActivity
because both fragments can live independently from each other, then:
HostActivity
that implements a listener invoked from ListFragment for adding/replacing the ContentFragment
My question is: when ListFragment is instead hosted from ListFragmentActivity, how to avoid to duplicate the code present in the HostActivity into ListFragmentActivity.
Guess I am missing something, thanks in advance.
Get rid of ListFragmentActivity. Have HostActivity handle the case where there is either one or both fragments. Then, by definition, there is no code duplication. See: https://github.com/commonsguy/cw-omnibus/tree/master/LargeScreen/EU4You

ViewPager inside fragment issue

I'm having problem with view pager which is inside of some fragment. To work with view pager I'm using FragmentPagerAdapter. And sure I'm getting java.lang.IllegalStateException: Recursive entry to executePendingTransactions exception when trying to run my app.
Most people say it's impossible to have FragmentPagerAdapter inside other fragment, so I'm sorry if this question is invalid. I was just hoping that maybe someone has some fresh ideas about this problem and its possible solution.
P.S. I've got an advice to set adapter inside of AsyncTask (I can clarify this case if someone needs it), but this solution has some bugs related with restoring such fragment state, so it's working for me.
Starting with Android 4.2, you can use nested fragments. The Android support library now also includes support for this, so you can use it with older Android versions.
The basic pattern looks like this:
Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();

Categories

Resources