0
我有一個關於片段主題的諮詢,希望我可以幫助我,我創建了兩個佈局用於控制方向(縱向和橫向),並且都調用同一個類的片段。 當我在方向(縱向)運行一些例程是可以的,但是當方向發生變化時,對象會「重新啓動」或「重新生成」,我讀到可以通過使用savedInstanceState()驗證生成的實例來糾正這個問題。 ,但感謝您的幫助有兩種佈局的片段
問候。
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();
}
}
}
對,我會回顧一下我所指的。我發表評論,謝謝 –