gpt4 book ai didi

java - 原始 fragment 不会进入堆栈后面,它仍然可见

转载 作者:行者123 更新时间:2023-12-02 00:22:20 26 4
gpt4 key购买 nike

我在使用 FragmentTransactions 时遇到一些问题。我已成功从 onClick 方法调用 fragment ,但原始 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);
}
}
<小时/>

来自ma​​in.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的代码<--原始 fragment

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的代码<--新 fragment

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);
}
}

最佳答案

您无法替换或删除 XML 中定义的 fragment - 它会成为布局的不可删除部分。但是,如果您在代码中定义了 fragment ,则可以随意替换和删除它们。

关于java - 原始 fragment 不会进入堆栈后面,它仍然可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10706615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com