I want to replace fragment but its add after previous fragment - android

I want to replace fragment but android app add new fragment after first fragment instead of replace fragment. And after that when i try to replace again app replace with fragment which one is added after first fragment. First fragment which was loaded on activity, remain static. Can any body help? I am using this code to replace fragment.
Fragment fr = fragment;
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frgRemote, fr);
ft.commit();
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.cerad.home.automation.RoomsActivity$PlaceholderFragment" >
<LinearLayout
android:id="#+id/lytHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/lytCamcontroller"
android:orientation="vertical" >
<TextView
android:id="#+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/stream_image"
android:layout_width="640px"
android:layout_height="480px"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/border"
android:contentDescription="#string/image_descriptor_video_streaming" />
<ScrollView
android:id="#id/lytCommandnControl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<fragment
android:id="#+id/frgRemote"
android:name="com.cerad.automation.DefaultRemoteFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="#+id/btnTVRemote"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_tv_remote" />
<Button
android:id="#+id/btnDefault"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="#string/button_default_control" />
<Button
android:id="#+id/btnACRemote"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_ac_remote" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

I have three fragments, i replace like this:
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new AlterFragment();
break;
case 2:
fragment = new ExtraFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
else {
}
}

You might want to re-read the documentation for FragmentTransaction.replace():
The first argument to replace(int containerViewId, Fragment fragment) is not the ID of the fragment's view, but the ID of the container of the fragment. Hence your call adds a new nested fragment (of the same type) inside the fragment view wrapper of the already existing fragment.
From FragmentTransaction.replace(id, frag, tag)'s documentation:
This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.
To fix this, you must give an ID to the LinearLayout containing your fragment, e.g.
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
and call replace() with the ID of the container.
ft.replace(R.id.fragment_container, fr);
It is also good practice to always assign unique tags to pre-defined fragments:
<fragment
android:id="#+id/frgRemote"
android:name="com.cerad.automation.DefaultRemoteFragment"
android:tag="remote_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
So that you can address multiple fragments by their unique tags, if necessary
ft.replace(R.id.fragment_container, fr, "remote_fragment");

Related

In a fragment if I press back button go to SettingsActivity.class

I have a activity_settings.xml file like below in this file I have 3 text views if I click one it will take me to a fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.crowderia.chat.SettingsActivity"
android:background="#color/black">
<TextView
android:id="#+id/tv_added_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Added Me" />
<TextView
android:id="#+id/tv_add_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Friends" />
<TextView
android:id="#+id/tv_my_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Friends" />
<FrameLayout
android:id="#+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
My SettingsActivity.class like below
onclick event for each text view
tv_added_me.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setFragment(new AddedMeFragment());
}
});
set fragment method
public void setFragment(Fragment f) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragmentTransaction.replace(R.id.settings_fragment, f);
fragmentTransaction.commit();
}
If I click each text view it will take me to each fragment but after I click back its not going to go to Settings Activity how can I make this work
Please help me Im new to these stuff
You should remove all the fragments from FragmentManager on the click of back button.
FragmentManager fm = getSupportFragmentManager();
for (Fragment f : fm.getFragments()) {
fm.beginTransaction().remove(f).commit();
}

Replacing a single fragment with multiple fragments

I have one activity (Navigation Controller) that controls multiple fragments. The initial screen the user sees is a single fragment placed into one container. When I click a button to a different fragment I need to add the first fragment to the backstack and add the two new fragments to different containers than the first one. The two fragments are a map fragment on top and a profile details fragment on the bottom Is this even possible?
The code is below.
Navigation Controller Home Fragment (this is the main screen when the app starts):
public void home(Bundle savedInstanceState){
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
CurrentFriendsFragment firstFragment = new CurrentFriendsFragment();
// Add the fragment to the 'fragment_container' FrameLayout
fragmentTransaction
.setCustomAnimations(R.animator.fade_in, R.animator.fade_out)
.replace(R.id.fragment_container, firstFragment, "firstFragment")
.commit();
}
}
Navigation Controller Profile Fragment:
#Override
public void onProfileButtonClicked() {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState1 != null) {
return;
}
MapFragment mapFragment = new MapFragment();
// Create a new Fragment to be placed in the activity layout
ProfileFragment profileFragment = new ProfileFragment();
// Add the fragment to the 'fragment_container' FrameLayout
fragmentTransaction
.setCustomAnimations(R.animator.slide_in_up, R.animator.slide_out_up, R.animator.slide_in_down, R.animator.slide_out_down)
.add(R.id.fragment_container_bottom_large, profileFragment)
.add(R.id.fragment_container_top_small, mapFragment)
.commit();
}
}
And here is the main activities xml file. Note the three different containers. "fragment_container" for full screen fragments, and "fragment_container_top_small", "fragment_container_bottom_large" for multiple fragments on one page.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.parse.starter.ViewControllers.NavigationController"
android:background="#color/palette_darkwhite">
<include layout="#layout/tool_bar"
android:id="#+id/toolbar_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#+id/toolbar_layout"
android:layout_above="#+id/bottom_navigation_navbar">
<RelativeLayout
android:id="#+id/fragment_container_top_small"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</RelativeLayout>
<FrameLayout
android:id="#+id/fragment_container_bottom_large"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar_layout"
android:layout_above="#+id/bottom_navigation_navbar">
</FrameLayout>
<FrameLayout
android:id="#+id/fragment_container_popup"
android:layout_width="275dp"
android:layout_height="275dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation_navbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/palette_lightwhite">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:background="#null"
app:srcCompat="#drawable/collpaseup" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<LinearLayout android:id="#+id/thumbnail2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_picture_navbar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
app:civ_border_color="#FF000000"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail2"
android:layout_toRightOf="#+id/thumbnail2"
android:textColor="#color/palette_primarycolor"
android:id="#+id/nameLabel"
android:text="Name"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/nameLabel"
android:layout_toRightOf="#+id/thumbnail2"
android:textColor="#color/palette_primarycolor"
android:id="#+id/locationLabel"
android:text="Location"
android:textSize="12sp"
android:textStyle="bold"/>
</RelativeLayout>
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
Have 3 containers and use View.GONE to make the 1st disappear and then View.VISIBLE to make the other 2 appear.

Frame layout inside a fragment

I am working in a Fragment. it contains 2 Buttons and a FrameLayout:
<FrameLayout 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"
tools:context="fragments.Fragment_Book_A_Cam">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/bt_Public"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:background="#drawable/red"
android:text="Public Jobs "
android:textColor="#FFFFFF" />
<Button
android:id="#+id/bt_Active"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:background="#drawable/black"
android:text="Active Jobs"
android:textColor="#FFFFFF" />
</LinearLayout>
<FrameLayout
android:id="#+id/Fragment_Jobs_BookACam"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
I want to load another Fragment in the FrameLayout on a Button click and don't want to replace the main FrameLayout.
All the methods I have used either crash or replace the main Fragment.
bt_Public.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// fragment = new Fragment_MyCloudVideo();
// FragmentManager fragmentManager = ;
// FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.replace(R.id.Fragment_MyVideos, fragment);
// fragmentTransaction.commit();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_Public_Jobs fragment = new Fragment_Public_Jobs();
fragmentTransaction.addToBackStack("xyz");
fragmentTransaction.hide(Fragment_Book_A_Cam.this);
fragmentTransaction.add(android.R.id.content, fragment);
fragmentTransaction.commit();
}
});
You never seem to use the Fragment_Jobs_BookACam FrameLayout id
If you want to show Fragments inside Fragments, you need to use the method to get the child FragmentManager
First of all correct the errors present in your xml file and try replace() method of fragmentTransaction instead add().

Replacing fragments on click of button android

I am trying to create a swappable fragements on my android app
Only half the part of fragment seems to be replacing
screenshot:
The xml file for the main is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
tools:context=".MainActivity"
android:id="#+id/contentlayout">
<fragment
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/header_menufragment"
class="com.example.www.newapp.header_menu" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/dynamicfragment"
class="com.example.www.newapp.mainfragment"/>
</LinearLayout>
the whole dynamicfragment is supposed to be replaced by projectfragment on click of Project button.
project.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager()
.beginTransaction()
.replace(R.id.dynamicfragment, new projectfragment())
.addToBackStack(null)
.commit();
}
});
the projectfragment class has
public class projectfragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_project, container,false);
}
}
the layout file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="#color/colorDarkRed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight=".5">
<ImageButton
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<ImageButton
android:id="#+id/img3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/imgfile"/>
</LinearLayout>
Kindly help me solve this issue.
You cannot replace a fragment defined statically in the layout file. You can only replace fragments that you added dynamically via a FragmentTransaction.
Use FrameLayout as container and replace Fragment inside that.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<FrameLayout android:id="#+id/fragment_container" android:layout_weight="1"
android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
On Activity add Fragment same as you do:
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new projectfragment())
.addToBackStack(null)
.commit();
For more info refer below link:
link1
link2
that worked for me try this way
WishaFriend ff = new WishaFriend();
android.app.FragmentManager fm = getActivity().getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(((ViewGroup) getView().getParent()).getId(), ff);
ft.commit();
dialog.dismiss();
try add this
ft.replace(((ViewGroup) getView().getParent()).getId(), ff);

Fragment not being replaced correctly

I created an activity with a navigation drawer and a FrameLayout. This frame layout should contain the fragment which corresponds the selected navigation item. So I followed the instructions in android tutorials, but it doesn't work.
Problem:
It seems to me the fragments are only added and not replaced. What am I doing wrong?
My activity xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainViewDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/mainViewContentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/drawer_content_padding" >
</FrameLayout>
<ListView android:id="#+id/mainViewDrawerList"
android:layout_width="#dimen/drawer_size"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="none"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#color/drawer_background"
/>
</android.support.v4.widget.DrawerLayout>
my first fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_horizontal_margin"
android:id="#id/fragmentProfile">
<TextView
style="#android:style/TextAppearance.Small"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/mainViewProfileGivenNameLabel"
/>
<TextView
android:id="#+id/profileGivenNameTextView"
style="#android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
my second fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#id/fragmentProtocol">
<ListView android:id="#+id/protocolTableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:transcriptMode="alwaysScroll"
android:cacheColorHint="#00000000"
android:listSelector="#android:color/transparent"/>
</LinearLayout>
my transition method in the activity:
private void switchToFragment(int navigationId, boolean addToBackStack){
FragmentManager fragmentManager = getFragmentManager();
String fragmentTag = null;
boolean fragmentCreated = false;
switch(navigationId) {
case NavigationItemProvider.NAVIGATIONITEM_PROFILE:
fragmentTag = NAVIGATIONTAG_PROFILE;
break;
case NavigationItemProvider.NAVIGATIONITEM_PROTOCOL:
fragmentTag = NAVIGATIONTAG_PROTOCOL;
break;
}
// determine if the fragment is already instanciated otherwise create
FragmentBase fragment = (FragmentBase)fragmentManager.findFragmentByTag(fragmentTag);
if (fragment == null){
if (fragmentTag == NAVIGATIONTAG_PROFILE) {
fragment = new ProfileFragment();
fragmentCreated = true;
}
else if(fragmentTag == NAVIGATIONTAG_PROTOCOL) {
fragment = new ProtocolFragment();
fragmentCreated = true;
}
}
// switch to fragment
if (fragment.isVisible()){
return;
}
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (fragmentCreated) {
transaction.replace(R.id.mainViewContentFrame, fragment, fragmentTag);
}
else{
transaction.show(fragment);
}
if(addToBackStack)
{
transaction.addToBackStack(null);
}
transaction.commit();
}
solved the problem:
In onCreate I returned the result of super.onCreate and not the result of Layoutinfalter.inflate and in addition I didn't call inflate with last parameter set to false.

Categories

Resources