i have an activity with button on it and when a click i call the fragment with another button on fragment. but when click on fragment's button i cant call a second fragment. this is my source, pretty simple:
activity_main.xml:
<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="horizontal"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_click"
android:text="Call Fragment"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:onClick="onClick"
/>
</LinearLayout>
fragment1.xml
<?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"
android:background="#f0f0f0"
android:orientation="vertical" >
<TextView
android:id="@+id/fragment1"
android:text="Fragment 1"
android:textSize="25sp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:id="@+id/btn_frag2"
android:text="Call Fragment 2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
fragment2.xml
<?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"
android:background="#f0f0f0"
android:orientation="vertical" >
<TextView
android:id="@+id/fragment2"
android:text="Fragment 2"
android:textSize="25sp"
android:gravity="center_vertical|center_horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View v) {
Fragment1 fragment1 = new Fragment1();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, fragment1);
fragmentTransaction.commit();
}
}
Fragment1.java
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
public void onClick2(View view) {
Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.commit();
}
}
Fragment2.java
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
what is wrong in my code?
I think now Fragment nesting is available just update the back computability jar
now lets dig in the problem it self .
public void onClick2(View view) {
Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.commit();
}
I think the R.id.fragment1
belongs to a TextView
which is not a good place to include child views in because its not a ViewGroup
, you can remove the textView
from the xml and replace it with a LinearLayout
lets say and it will work , if not tell me what the error .
fragment1.xml
<?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"
android:background="#f0f0f0"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fragment1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<Button
android:id="@+id/btn_frag2"
android:text="Call Fragment 2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Update for the error in the comment
public class Fragment1 extends Fragment implements OnClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container, false);
((Button) v.findViewById(R.id.btn_frag2)).setOnClickListener(this);
return v;
}
public void onClick(View view) {
Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
If you want to replace the entire Fragment1 with Fragment2, you need to do it inside MainActivity, by using:
Fragment1 fragment2 = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, fragment2);
fragmentTransaction.commit();
Just put this code inside a method in MainActivity, then call that method from Fragment1.
This is my answer which solved the same problem. Fragment2.java is the fragment class which holds the layout of fragment2.xml.
public void onClick(View v) {
Fragment fragment2 = new Fragement2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
I would use a listener. Fragment1 calling the listener (main activity)
This code works for me to call the parent_fragment method from child_fragment.
ParentFragment parent = (ParentFragment) activity.getFragmentManager().findViewById(R.id.contaniner);
parent.callMethod();
In MainActivity
private static android.support.v4.app.FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
}
public void secondFragment() {
fragmentManager
.beginTransaction()
.setCustomAnimations(R.anim.right_enter, R.anim.left_out)
.replace(R.id.frameContainer, new secondFragment(), "secondFragmentTag").addToBackStack(null)
.commit();
}
In FirstFragment call SecondFrgment Like this:
new MainActivity().secondFragment();
Just do that: getTabAt(index of your tab)
ActionBar actionBar = getSupportActionBar();
actionBar.selectTab(actionBar.getTabAt(0));