Android Fragment replacement issue - android

i am using the following layout for fragment transaction. i am replacing the fragment1
with new fragments by using the following code.i am attaching the layout file also for your reference. i am executing the below code for every listitem click.
Code:
Fragment newFragment = new Grades();
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.fragment1, newFragment);
// Commit the transaction
transaction.commit();
Fragment newFragment = new Teachers();
android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.fragment1, newFragment);
// Commit the transaction
transaction.commit();
<LinearLayout
android:id="#+id/slidingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:orientation="vertical"
android:background="#android:color/white"
>
<fragment
android:id="#+id/fragment1"
android:name="com.example.slideoutmenu.Item3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</Linearlayout>
my doubt is should i replace the fragment1 everytime when i show a new fragment or should i replace the existing fragment if so how can i replace the existing fragment.

To replace the fragment:-
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
YourFragment yourFragment = new YourFragment();
fragmentTransaction.replace(R.id.top_layout, yourFragment ); // top_layout is parent layout / viewgroup where you want to place your new fragment
fragmentTransaction.commit();

Related

Android - Call a fragment inside tab

I try to create an application that has two pages. First page is a tabbed page and second page is a detail page.
I create a tapped page using this guide. I have a mainactivity that contains view pager and 3 tabs(Tab1, Tab2, Tab3). Tab3 contains a button to navigate detail page. Each tab has own .java(extends Fragment) and .xml file. I edit fragmentThree.xml layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragmentThree"]>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/goToDetail"
android:text="GO TO DETAIL" />
I create a new fragment for detail page. I set onclick method inside of Tab3.java;
goToDetail.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.mainActivity, new Detail());
transaction.commit();
}
});
When I press the button, it doesn't do anything. I don't replace the fragment.
Use you viewpager id instead of main layout id, where you set the tab layout fragment.
transaction.replace(R.id.viewpager, new Detail());
use Frame layout in Main Activity
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent">
In clicklistiner :-
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.parent, new Detail());
transaction.commit();

Fragment doesn't work using fragment tag, works in framelayout

Fragment doesn't work when using fragment tag in XML layout. But, after replace fragment tag to Framelayout it works fine without making any change in my code.
When i use the code below fragment doesn't change/work.
<fragment
android:id="#+id/fragment_container"
android:name="com.example.user.example.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btnPressMe"
/>
But, when i replace the above code with the code below it changes/works.
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btnPressMe">
</FrameLayout>
Java code:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.fragment_container, fragment2);
fragmentTransaction.commit();
if you want to infalte to another fragment you must use
getChildFragmentManager() instead of getFragmentManager()
FragmentManager fobject = getChildFragmentManager();
FragmentTransaction transaction = fobject .beginTransaction();
fragment_container= fobject .findFragmentById(R.id.fragment_container);
if (fragment_container!= null) {
transaction.replace(R.id.fragment_container, yournewfragment);
transaction.commit();
}
transaction.commit();

issue with fragment painting over other fragment. How do I replace a fragment?

I have code like this to take my currently shown fragment in my activity and show a different fragment. Sometimes the first view is displayed on top of the other view.
What could cause that? Is there a better way to do this?
android.app.FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.remove(from);
fragmentTransaction.commit();
getFragmentManager().executePendingTransactions();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container, to);
if(showBackButton) {
fragmentTransaction.addToBackStack(to.toString());
}
fragmentTransaction.commit();
You might try the replace command instead. Something like:
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment_container, iFrag);
transaction.commit();
The fragment_container is just a FrameLayout inside its own XML file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
android:background="#888888" />
iFrag is an instantiated class that extends Fragment.

How can I add a fragment from within a viewpager that covers the viewpager tabs

Currently I have a viewpager setup according to the documentation. Within the first fragment tab I have setup a recycler view that contains a list of cardviews. Within each cardview I have a button which should open a new fragment. I would like this new fragment to overlap the action bar tabs (something similiar to how facebook comments work when you click the comment button). I am not sure how to get it working. I created an extra framelayout and relative layout above my viewpager hoping that it would overlap, however this doesn't seem to work. How can I have my new fragment cover the action bar tabs as well? I am not asking how to replace the current viewpager fragment if that makes sense.
I filled the new fragment with a red background, the result after envoking the onclick listener is shown here: http://i.imgur.com/yHYpzGr.png
In my onclick listener I have:
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, commentFragment,"comment_fragment_tag");
fragmentTransaction.commit();
My main activity layout that contains the viewpager:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/main_activity">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</FrameLayout>
Try this .. hope it works
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, commentFragment,"comment_fragment_tag");
fragmentTransaction.commit();
or
Fragment fr = new FragmentTwo() ;
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fr);
ft.commit();
hear fr is the new fragment that would be replaced

Fragment not added to backstack

I'm having trouble with getting Fragments and the backstack to work.
I've got a layout with a FrameLayout for holding various fragments and another fragment:
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/filterableListContainer"
android:layout_weight="50">
</FrameLayout>
<fragment class="com.facetoe.remotempd.fragments.PlayerBarFragment"
android:id="#+id/playerBarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:layout="#layout/player_bar"/>
</LinearLayout>
When I start the Activity that uses this layout I add the fragment to the FrameLayout like so:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
if (findViewById(R.id.filterableListContainer) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
ArtistListFragment listFragment = new ArtistListFragment();
// Add the fragment to the 'fragment_container' FrameLayout
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.filterableListContainer, listFragment);
ft.addToBackStack(null);
ft.commit();
} else {
Log.e(TAG, "Couldn't find filterableListFragment");
}
}
When the user clicks an item, I attempt to replace the fragment with this code:
ArtistAlbumsListFragment fragment = new ArtistAlbumsListFragment(getActivity(), albums);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.filterableListContainer, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
However when I press the back button I get returned to the home screen. Can anyone tell me where I'm going wrong?
Ok I fixed it. When I created the Activity in Intellij it created a subclass of ActionBarActivity. Removing ActionBarActivity and replacing it with Activity made the fragments transitions work as expected.
Dont add ft.addToBackStack(null) this statement.
ArtistAlbumsListFragment fragment = new ArtistAlbumsListFragment(getActivity(), albums);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.filterableListContainer, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();

Categories

Resources