活动:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment1 fragment = new Fragment1();
Fragment2 fragment2 = new Fragment2();
transaction.replace(R.id.Fragment1, fragment);
transaction.addToBackStack(null);
transaction.commit();
FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.Fragment1, fragment2);
transaction2.addToBackStack(null);
transaction2.commit();
Run Code Online (Sandbox Code Playgroud)
视图中的代码:
<fragment
android:id="@+id/Fragment1"
android:name="com.landa.fragment.Fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/include1" />
Run Code Online (Sandbox Code Playgroud)
问题是,内容并没有真正被替换 - 它被置于顶部(因此它重叠).
当我单击返回时,第一个片段会正确显示(没有第二个片段),但最初两个片段都是可见的(我只希望最后一个片段可见).
我在这里错过了什么?
请查看下面的示例代码
@Override
protected void onPause() {
...some code here...
super.onPause();
}
Run Code Online (Sandbox Code Playgroud)
和
@Override
protected void onPause() {
super.onPause();
...some code here...
}
Run Code Online (Sandbox Code Playgroud)
当我询问代码差异时,我并不是指执行流程,这是明显的.
那么这些代码之间的真正区别是什么?建议何时在 super()通话前使用您的代码,以及何时在 super()通话后使用您的代码?我想有些情况确实很重要.
用户单击一个按钮,该按钮会打开一个片段对话框,该对话框会像这样膨胀片段:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(
R.layout.monday_fragment, null);
builder.setView(view).setTitle("Homework Due Monday")
.setNegativeButton("Dismiss", null);
AlertDialog dialog = builder.create();
dialog.show();
Run Code Online (Sandbox Code Playgroud)
这是指定布局的xml(monday_fragment.xml),其中对要膨胀的片段的引用位于:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<fragment
android:id="@+id/monday_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
片段对话框中的片段然后在其OnCreateView中膨胀自己的布局:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
swipe = inflater.inflate(R.layout.day_homework_fragment, container,
false);
lView = (ListView) swipe.findViewById(R.id.listView1);
progress = (ProgressBar) swipe.findViewById(R.id.progress);
lView.setVisibility(View.GONE);
return swipe;
}
Run Code Online (Sandbox Code Playgroud)
以下是片段中膨胀的"day_homework_fragment.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:orientation="vertical"
android:layout_gravity="center"
android:background="@color/light_background"> …Run Code Online (Sandbox Code Playgroud) android illegalstateexception android-fragments inflate-exception