gpt4 book ai didi

Android:屏幕方向改变后 fragment 恢复

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:43 25 4
gpt4 key购买 nike

我从 fragment 开始,我遇到了一个问题。我不想在屏幕旋转后恢复我的 fragment 。我的应用程序看起来像这样:在横向上,我在左侧区域有一个按钮,它更新右侧区域的标签。如果在纵向模式下,我正在导航到一个新 Activity 。但是,我不想在旋转后保持 fragment 状态。代码如下所示:

左侧区域 fragment :

public class ListFragment extends Fragment implements OnClickListener {

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

View view = inflater.inflate(R.layout.fragment_list, container, false);
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(this);

return view;
}

@Override
public void onClick(View v) {

switch (v.getId()) {

case R.id.button1:
updateDetail();
break;

}
}

public void updateDetail() {
String newTime = String.valueOf(System.currentTimeMillis());
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(newTime);
} else {
Intent intent = new Intent(getActivity().getApplicationContext(),
DetailActivity.class);
intent.putExtra("value", newTime);
startActivity(intent);
}
}

右侧区域 Activity :

public class DetailActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}

if (savedInstanceState == null) {
setContentView(R.layout.activity_detail);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String s = extras.getString("value");
TextView view = (TextView) findViewById(R.id.detailsText);
view.setText(s);
}
}
}

右侧区域 fragment :

public class DetailFragment extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}

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

View view = inflater
.inflate(R.layout.fragment_detail, container, false);

return view;
}

public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.detailsText);
view.setText(item);
}

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

FragmentManager fm = getFragmentManager();

DetailFragment fragment = (DetailFragment)fm.findFragmentById(R.id.detailFragment);

if (fragment == null) {
fragment = new DetailFragment();
fragment.setTargetFragment(this, 0);
fm.beginTransaction().add(R.id.detailFragment, fragment).commit();
}
}

我可能做错了什么?

最佳答案

您有几个选项可以在 couple 中使用 Bundle save 的方法和 restore fragment 的状态。

或者您可以使用 setRetainInstance方法:

onCreate(Bundle save)
{
super.onCreate(save);
setRetainInstance(true);
}

请记住,setRetainInstance 方法不适用于后台堆栈中的 fragment 。

关于Android:屏幕方向改变后 fragment 恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12831344/

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