Android:屏幕方向更改后片段还原

时间:2012-10-11 02:33:48

标签: android android-fragments

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

左区域片段:

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

}

我可能做错了什么?

2 个答案:

答案 0 :(得分:-1)

您可以使用couple方法中的saverestore片段的状态使用Bundle。

或者您可以使用setRetainInstance方法:

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

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

答案 1 :(得分:-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

确保您的片段状态不会丢失