簡體   English   中英

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

[英]Android: fragment restore after screen orientation change

我從片段開始,現在面臨一個問題。 屏幕旋轉后,我將無法恢復片段。 我的應用程序如下所示:在橫向視圖上,左側區域有一個按鈕,該按鈕在右側區域更新了一個標簽。 如果是縱向模式,我將導航到一個新活動。 但是,旋轉后我不會保持片段狀態。 代碼如下:

左側區域片段:

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

}

我可能做錯了什么?

你可以使用捆綁的幾個選項對夫婦的方法來保存恢復片段的狀態。

或者,您可以使用setRetainInstance方法:

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

請記住,setRetainInstance方法不適用於Backstack中的片段。

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM