gpt4 book ai didi

android - 如何实现saveFragmentInstanceState?

转载 作者:行者123 更新时间:2023-11-30 03:52:45 27 4
gpt4 key购买 nike

谁能告诉我如何实现 saveFragmentInstanceState() 或其他一些方法来在按下后退按钮时检索 fragment 实例。我使用自己的堆栈作为 fragment ,即下面的代码:

public class stackA extends ActivityInTab {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.i("StackA", "onCreate");

if(savedInstanceState == null){
navigateTo(new fragmentA());
}

}
}

接下来是 ActivityInTab 类的实现。我认为此类必须实现用于保存和检索 fragment 状态的方法,但我仍然找不到如何执行此操作的方法。

abstract class ActivityInTab extends FragmentActivity { 

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

Log.i("ActivityInTab", "onCreate");
}

protected void navigateTo(Fragment newFragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
@Override
public void onBackPressed() {

Log.i("ActivityInTab", "onBackPressed");

FragmentManager manager = getSupportFragmentManager();
if (manager.getBackStackEntryCount() > 1) {
super.onBackPressed();
} else {
// Otherwise, ask user if he wants to leave :)
//showExitDialog();
super.onBackPressed();
}
}
}

最后,这是 fragment 的实现:

public class fragmentA extends Fragment {

private LinearLayout ll;
private FragmentActivity fa;

private String textViewText;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

Log.i("Fragment A", "onCreateView");

fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.fragmenta, container,
false);
Button next = (Button) ll.findViewById(R.id.button1);
Button randomBtn = (Button) ll.findViewById(R.id.random_button);
final TextView randomText = (TextView) ll
.findViewById(R.id.random_textview);
next.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

((ActivityInTab) getActivity()).navigateTo(new
fragmentB());
}
});

randomBtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
randomText.setText(String.valueOf(Math.random()));
textViewText = randomText.getText().toString();
}
});

if (savedInstanceState != null) {
randomText.setText(savedInstanceState.getString("TextView"));
}

return ll;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

// setRetainInstance(true);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("TextView", textViewText);
}
}

我只能使用 onSaveInstanceState(Bundle outState) 为方向更改保存实例状态,但不能为后退按钮保存实例状态。

如果有任何建议,我将不胜感激。

最佳答案

在 onBackPressed 方法中,您可以访问 FragmentManager,它可以使用 FragmentManager 方法“findFragmentById”或“findFragmentByTag”为您检索任何 fragment 。

根据添加 fragment 的方式,您可以使用这两种方法中的任何一种来直接访问要保存其状态的任何 fragment 。

关于android - 如何实现saveFragmentInstanceState?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13903216/

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