gpt4 book ai didi

java - 方向改变时处理多个 fragment

转载 作者:行者123 更新时间:2023-11-29 20:08:01 25 4
gpt4 key购买 nike

我有一个 MainActivity,我想将一个包含 3 个按钮的 fragment 附加到该 Activity 。单击按钮 1 时,它应将此 fragment 替换为另一个 fragment 。但是当我改变方向时,当前 fragment 和旧 fragment 都被附加了。有人可以帮我解决这个问题吗?

以下是我的MainActivity.java:

public class MainActivity extends AppCompatActivity implements OnButtonsClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


getSupportFragmentManager().beginTransaction()
.add(R.id.mainactivity, new FragmentHomepage(), "aboutMe")
.commit();

}




@Override
public void button1Action() {
FragmentAboutMe fragmentAboutMe=new FragmentAboutMe();
getSupportFragmentManager().beginTransaction()
.add(R.id.mainactivity,fragmentAboutMe)
.commit();
}

@Override
public void button2Action() {
Intent intent =new Intent(this,ActivityMasterDetail.class);
startActivity(intent);
}

@Override
public void button3Action() {
Intent intent =new Intent(this,ActivityViewPager.class);
startActivity(intent);
}
}

这是我的FragmentHomepage.java:

public  class FragmentHomepage extends Fragment {
private static final String ARG_SECTION_NUMBER ="section number";
OnButtonsClickListener onButtonsClickListener;
public static FragmentHomepage newInstance(int sectionNumber){
FragmentHomepage fragmentHomepage=new FragmentHomepage();
Bundle args=new Bundle();
args.putInt(ARG_SECTION_NUMBER,sectionNumber);
fragmentHomepage.setArguments(args);
return fragmentHomepage;
}
public FragmentHomepage(){}

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
onButtonsClickListener= (OnButtonsClickListener) context;
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final Button button1= (Button) getActivity().findViewById(R.id.aboutMe);
final Button button2= (Button) getActivity().findViewById(R.id.task2);
final Button button3= (Button) getActivity().findViewById(R.id.task3);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
onButtonsClickListener.button1Action();
}
});
button2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
onButtonsClickListener.button2Action();
}
});
button3.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
onButtonsClickListener.button3Action();
}
});

}

@Nullable
@Override
public View onCreateView(final LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View rootView=null;
rootView =inflater.inflate(R.layout.fragment_homepage,container,false);
return rootView;
}
}

我的第二个 Activity 如下(在 FragmentAboutMe.java 中):

public class FragmentAboutMe extends Fragment {
int counters=0;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null)counters=0;
else counters=savedInstanceState.getInt("count");

}

@Override

public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("count",13);

}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_aboutme,container,false);
}
}

最佳答案

在您的 onCreate() 方法中检查 fragment 是否已经存在。 Android 会在方向更改时自动恢复 fragment 管理器状态,因此在方向更改时,您在单击按钮时添加的 fragment 将自动添加。因此,如果您未经检查就在 onCreate() 中添加新 fragment ,这将导致添加 2 个 fragment 。这是导致问题的原因。

FragmentManager fm = getSupportFragmentManager();

if (fm.findfragmentById(R.id.mainactivity) == null) {
FragmentHomepage fragment = new FragmentHomepage ();
fm.beginTransaction().add (R.id.mainactivity, fragment).commit();
}

关于java - 方向改变时处理多个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35424628/

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