Answer the question
In order to leave comments, you need to log in
How to change fragment A to fragment B with redrawing B, if it has already been created. And then B to A without redrawing?
Faced such a problem. There is a graphics layer for fragments A and B:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:id="@+id/list_manager_container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
public void switchFragment(android.support.v4.app.Fragment switchtofrag,
android.support.v4.app.Fragment switchfromfrag,
int switcharea,
boolean redraw) {
android.support.v4.app.FragmentTransaction ft = mVCollectionPageAdapter.mFM.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
if (switchtofrag.isAdded()) {
if (redraw) {
ft.detach(switchtofrag);
ft.attach(switchtofrag);
}
ft.show(switchtofrag);
ft.hide(switchfromfrag);
} else {
ft.hide(switchfromfrag);
ft.add(switcharea, switchtofrag);
ft.show(switchtofrag);
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
Answer the question
In order to leave comments, you need to log in
On a vskidka: when you click on the button in fragment B, call getActivity().getSupportFragmentManager().popBackStack();
I correctly understood that when you click on the button in fragment B, fragment A should appear in its place , in fact, you need to go back?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/fragment">
</FrameLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, new MainActivityFragment()).commit();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
btn = (Button) view.findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment, new BlankFragment()).addToBackStack(null).commit();
}
});
return view;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
btn = (Button) view.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().popBackStack();
}
});
return view;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question