我对片段主题进行了咨询,希望可以帮助我,我创建了两个用于控制方向的布局(纵向和横向),并且都调用同一个类片段。当我在一个方向(纵向)运行一些例程时是可以的,但是当方向发生变化时,对象被“重新启动”或“重新生成”,我读到这可以通过使用 saveInstanceState() 验证生成的实例来纠正,但感谢您的帮助
问候。
PS:对不起我的英语
public class AlbumDetail extends Activity {
....
private Bundle bundle = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.bundle = savedInstanceState;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.albumdetail_p);
initUI();
} else {
setContentView(R.layout.albumdetail_l);
initUI();
}
lstSongs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
pos = position;
if (bundle == null){
PlayFragment fragment = (PlayFragment) getFragmentManager().findFragmentById(R.id.fragmentPlay);
if (fragment != null && fragment.isInLayout()) {
fragment.getPlaySong(pos,isRandom,songs);
}
}
}
});
}
private void initUI(){
/* Initialize variables */
}
}
public class PlayFragment extends Fragment{
private Intent playIntent;
private View view;
....... //Variables
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmentplay, container, false);
return view;
}
public void onSaveInstanceState(Bundle bundle){
super.onSaveInstanceState(bundle);
}
public void initUI(){
/* Initialize variables*/
}
public void getPlaySong(int position, boolean isRandom, Object lstSong) {
initUI();
try {
playIntent = new Intent(getActivity().getBaseContext(), PlaySongServiceFragment.class);
playIntent.putExtra("pos", position);
playIntent.putExtra("rnd", isRandom);
playIntent.putExtra("list", (Serializable) lstSong);
getActivity().startService(playIntent);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}