我遇到了 FragmentTransactions 的一些问题。我已经成功地从 onClick 方法调用了一个片段,但原始片段mainFragment
不会“消失”或回到堆栈的后面。我遵循了关于 Fragment Transcation 的 Android 开发教程。我认为问题出在我的Survey.java
班级或Details.java
班级。任何帮助表示赞赏。
更新:(在下面添加照片)
之前: http: //imm.io/q9vt
点击“调查按钮”之后:http: //imm.io/q9wf
请参阅以下所有代码:
来自Main.java的代码
package mycompany.nst;
import mycompany.nst.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Fragment _surveyFrag = new Survey();
public void focusFragment(Fragment mainFragment) {
FragmentManager FragMgr = getFragmentManager();
FragmentTransaction transaction = FragMgr.beginTransaction();
try {
transaction.replace(R.id.mainFragment, _surveyFrag);
transaction.addToBackStack(null);
transaction.commit();
} catch(Exception e){};
transaction = null;
FragMgr = null;
}
public void survey_onClick(View view) {
Log.i("onClick", "SURVEY BEGIN");
focusFragment(_surveyFrag);
}
}
来自main.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/hsdarker"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/listFragment"
android:name="mycompany.nst.ListFragment"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="mycompany.nst.ListFragment" >
<!-- Preview: layout=@layout/list -->
</fragment>
<fragment
android:id="@+id/mainFragment"
android:name="mycompany.nst.Details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/listFragment"
class="mycompany.nst.Details" >
<!-- Preview: layout=@layout/details -->
</fragment>
</RelativeLayout>
来自Details.java的代码 <-- 原始片段
package mycompany.nst;
import mycompany.nst.R;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Details extends Fragment {
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater LIdetails, ViewGroup VGdetails, Bundle SaveInstancedState) {
Log.i("DetailsFragment", "onCreateView");
// Inflate the layout for this fragment
return LIdetails.inflate(R.layout.details, VGdetails, false);
}
}
来自Details.java的代码 <-- 新片段
package mycompany.nst;
import mycompany.nst.R;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Survey extends Fragment {
/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("SurveyFragment", "onCreateView");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.survey, container, false);
}
}