4

我从片段开始,我面临一个问题。我不想在屏幕旋转后恢复我的片段。我的应用程序如下所示:在横向上,我在左侧区域有一个按钮,用于更新右侧区域的 a 标签。如果在纵向模式下,我正在导航到一个新活动。但是,我不想在旋转后保持片段状态。代码如下所示:

左侧区域片段:

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

}

右区活动:

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

}

右侧区域片段:

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

}

我可能做错了什么?

4

2 回答 2

-1

您有几个选项可以通过几种方法使用 Bundle 来保存恢复片段的状态。

或者您可以使用setRetainInstance方法:

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

请记住,setRetainInstance 方法不适用于 backstack 中的片段。

于 2013-05-24T14:23:51.307 回答
-2
@Override
public void onConfigurationChanged(Configuration newConfig) {
    int orientation = getResources().getConfiguration().orientation;
    switch (orientation) {
    case Configuration.ORIENTATION_LANDSCAPE:
        getSupportFragmentManager().beginTransaction().replace(R.id.detailFragment, new DetailFragment()).commitAllowingStateLoss();
        getSupportFragmentManager().beginTransaction().remove(new DetailFragment()).commitAllowingStateLoss();
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        getSupportFragmentManager().beginTransaction().replace(R.id.detailFragment, new DetailFragment()).commitAllowingStateLoss();
        getSupportFragmentManager().beginTransaction().remove(new DetailFragment()).commitAllowingStateLoss();
        break;
    }
    super.onConfigurationChanged(newConfig);
}

您可以通过执行检查onConfigurationChange并确保您的片段状态不会丢失commitAllowingStateLoss

于 2013-05-24T15:26:41.867 回答