Android Backstack with Nested/Child Fragments - android

I've got a problem with the way backstack of nested Fragments and would be most grateful for any help that is offered.
I have Fragment A and Fragment B. Fragment A contains an other Fragment (ButtonFragment). In the onCreate of the Activity I load Fragment A, than I switch to Fragment B. When I go back to a Fragment A (out of the backstack) I get following Error.
Exception dispatching finished signal.
E/MessageQueue-JNI(6330): Exception in MessageQueue callback:
handleReceiveCallback
E/MessageQueue-JNI(6330): android.view.InflateException: Binary XML
file line #16: Error inflating class fragment
...
Caused by: java.lang.IllegalArgumentException: Binary XML file line
16: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for
com.example.fragmentnavigation.MainActivity$ButtonFragment
Without the subfragment the navigation works. Maybe I have to add some ChildFragmentManager handling, but I don't know what and where. I hope you can help me.
MainLayout
<LinearLayout 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:orientation="vertical" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.fragmentnavigation.MainActivity"
tools:ignore="MergeRootFrame" />
</LinearLayout>
Fragment A Layout
<LinearLayout 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.example.fragmentnavigation.MainActivity$FragmentA" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<fragment
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.fragmentnavigation.MainActivity$ButtonFragment"/>
</LinearLayout>
MainAcitivity
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new FragmentA()).commit();
}
}
...
private void switchToB() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, new BFragment());
ft.addToBackStack(null);
ft.commit();
}
Fragment A
public static class FragmentA extends Fragment {
public FragmentA() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
ButtonFragment
public static class ButtonFragment extends Fragment {
public ButtonFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.buttonfragment, container,
false);
return rootView;
}
}
Solution special thanks to #arun-kumar
a really good overview about Fragments
https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments
Fragment A Layout
<LinearLayout 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.example.fragmentnavigation.MainActivity$FragmentA" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<FrameLayout
android:id="#+id/child_fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
onCreate of Fragment A
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Fragment childFragment = new ButtonFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.child_fragment_container, childFragment).commit();
return rootView;
}

You have to add child fragment at runtime instead of declaring it in the layout in Fragment A Layout file. I got the same problem on implementing google maps.

I think the reason behind this exception is that you have got two view elements in your view with id=#+id/button
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name" />
and
<fragment
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.fragmentnavigation.MainActivity$ButtonFragment"/>
Try renaming one of them to something unique

Caused by: java.lang.IllegalArgumentException: Binary XML file line 16: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for com.example.fragmentnavigation.MainActivity$ButtonFragment
Two controls in your layout are of same id, you need to change id of any one of the control

Related

Dynamycally add multiple fragments in FrameLayout

I'm am trying to add dynamycally multiple fragments I'm creating to a framelayout, using FragmentTransaction's add() method. Each time I try to do it, Fragments are just replacing one an other.
Here is my code :
public class FragmentMerchandSearch extends Fragment {
public FragmentMerchandSearch() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_merchand_search, container, false);
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
for(int i = 0; i < 10; i++){
Fragment newFragment = new FragmentMerchandPresentation();
ft.add(R.id.container_merchand_presentation_for_search, newFragment);
}
ft.commit();
return view;
}
}
Here is the code of FragmentMerchandPresentation:
public class FragmentMerchandPresentation extends Fragment {
public FragmentMerchandPresentation(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_presentation_merchand, container, false);
return view;
}
}
Here is XML of fragment_merchand_search:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container_merchand_presentation_for_search">
</FrameLayout>
</LinearLayout>
</ScrollView>
And XML of fragment_presentation_merchand:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey !"/>
</LinearLayout>
You are adding your fragments inside a FrameLayout, this means one is above another.
To add them vertically, use a LinearLayout as a container.
Just change your fragment_merchand_search.xml to:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container_merchand_presentation_for_search"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
Just change this line:
ft.add(R.id.container_merchand_presentation_for_search, newFragment);
to this:
ft.replace(R.id.container_merchand_presentation_for_search, newFragment);

how to replace a fragment with another fragment in android studio

i am trying to replace a displaynews fragment with homescreen fragment but the fragment is being adding instead of replacing in the code here
content_main.xml
<?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:id="#+id/content_main"
android:orientation="vertical"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.ajitesh.newson.MainActivity"
tools:showIn="#layout/app_bar_main">
<fragment
android:id="#+id/home_fragment"
android:name="com.ajitesh.newson.DisplayNews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
mainacitivity.class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentClass=HomeScreen.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.home_fragment,fragment).commit();
displaynews.class
public class DisplayNews extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.displaynews, container, false);
}
}
displaynews.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:id="#+id/display_news"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="helllooooo"/>
</LinearLayout>
HomeScreen.class
public class HomeScreen extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_screen, container, false);
}
}
home_screen.xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:text="this is home page"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"/>
</LinearLayout>
Fragments cannot be replaced if hard-coded in xml, you should add it dynamically to replace a fragment with another.
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
source // example -- still getting problems? You may find a solution here

ViewPager image gallery inside a Fragment

Hello im new android developer and i try to do the next things with no success
i have Activity, inside the activity i have fragment and inside it i have ** view pager image galley**.
the problem is when i run the app with the activity with the fragment and and gallery inside it the app crash
but when i tried to run the activity with the gallery and the fragment separately they works fine
whats the problem?
here my code:
Activity
public class infoActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotel_info);
}
}
Activity XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<fragment
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="co.checkit.mobile.checkit.Model.gallery_fragment"
tools:layout="#layout/imagegalleryfragment"
android:id="#+id/fragmentid" />
</LinearLayout>
ass you can see i set the fragment inside the activity
Fragment class
public class Hotelsgallery_fragment extends Fragment {
ImageAdapter adapter;
ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.imagegalleryfragment, container, false);
viewPager = (ViewPager)myFragmentView.findViewById(R.id.viewpager);
adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
return myFragmentView;
}
}
i set the gallery inside the fragment class
Fragment XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/viewpager">
</android.support.v4.view.ViewPager>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView4"
android:layout_gravity="center_horizontal" />
</LinearLayout>
instead of putting fragment inside xml, put frame layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/frame_layout_id" />
</LinearLayout>
create fragment inside a activity, inflate using frame layout id
YourFragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.add(R.id.frame_layout_id, fragment);
ft.commit();

android view under fragment responding to tap

I have two fragments A and B.Fragment A has a view which when clicked opens Fragment B which occupies the frame layout in activity_main.xml.The frame layout originally has a recyclerview in it.But when simply tapped on Fragment B the recyclerview in activity_main.xml is responding.
Fragment A:
public class Motels extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.motels, container, false);
final Toolbar toolbar=(Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle("Motels");
final FragmentManager fragmentManager = getFragmentManager();
RippleView rippleView6=(RippleView) v.findViewById(R.id.ripple6);
rippleView6.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
#Override
public void onComplete(RippleView rippleView2) {
Log.d("Sample", "Ripple completed");
MotelVaik motelVaik= new MotelVaik();
FragmentManager fragmentManager6=getFragmentManager();
final FragmentTransaction transaction6= fragmentManager.beginTransaction().setCustomAnimations(R.anim.enter_from_left_, R.anim.exit_to_right,R.anim.enter_from_right,R.anim.exit_to_left);
transaction6.replace(R.id.frame, motelVaik);
transaction6.addToBackStack("vaik").commit();
}
});}}
Fragment B:
public class MotelVaik extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.motel_vaikkom,container,false);
return v;}}
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main"
>
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="450dp"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scrollbarStyle="insideOverlay"
>
</android.support.v7.widget.RecyclerView>
</FrameLayout>
</LinearLayout>
In the xml of fragment B, on the root element just add
android:clickable="true"
make sure the height of the layout is match_parent

Android FrameLayout is not replace by Fragments

This app contains activity_main.xml, mainactivit.java and one Fragment class.
activity_main.xml has one Button and FrameLayout inside RelativeLayout.
When the Button is clicked, FrameLayout should be replaced by fragments, but it doesn't get replaced.
activity_main.xml
<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=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="loadd"
android:text="Fragment No.2" />
<FrameLayout
android:id="#+id/maincontainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
mainactivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadd(View v) {
try {
Fragment fragment = new fr();
FragmentManager frgManager = getSupportFragmentManager();
FragmentTransaction trans = frgManager.beginTransaction();
trans.replace(R.id.maincontainer, fragment);
trans.commit();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "error " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Fragment class
public class fr extends Fragment{
public View OnCreateView(LayoutInflater inf,ViewGroup parent,Bundle save){
return inf.inflate(R.layout.send,parent, false);
}
}
send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 3" />
</LinearLayout>
There is no compile time or run time error, but when I click that Button the FrameLayout is not replaced by the Fragment.
Everything that you posted looks good, except you aren't overriding Fragment.onCreateView, instead you've made a typo.
What you have
public View OnCreateView(LayoutInflater inf, ViewGroup parent, Bundle save) {
return inf.inflate(R.layout.test, parent, false);
}
What you should have
#Override
public View onCreateView(LayoutInflater inf, ViewGroup parent, Bundle save) {
return inf.inflate(R.layout.test, parent, false);
}
Pay close attention to the name of the method. It should begin with a lowercase "o".

Categories

Resources