我使用setRetainInstance(true)
on SherlockListFragment
,并希望它在屏幕旋转期间不会破坏和重新创建自己。然而,事实并非如此。onActivityCreated
每次我执行旋转时它仍然会调用。
WatchlistFragment.java
package org.yccheok.jstock.gui;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockListFragment;
public class WatchlistFragment extends SherlockListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "WatchlistFragment onCreate");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "WatchlistFragment onActivityCreated");
// Not sure why. After I setRetainInstance to true, onActivityCreated is still
// called for each screen rotation changes.
setRetainInstance(true);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, getTimeConsumingData()));
}
public String[] getTimeConsumingData() {
Log.i(TAG, "WatchlistFragment getTimeConsumingData");
return new String[] { "A", "B", "C"};
}
private static final String TAG = WatchlistFragment.class.getSimpleName();
}
JStockFragmentActivity.java
package org.yccheok.jstock.gui;
import android.os.Bundle;
import android.util.Log;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class JStockFragmentActivity extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "JStockFragmentActivity onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.jstock_fragment_activity);
}
private static final String TAG = JStockFragmentActivity.class.getSimpleName();
}
jstock_fragment_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment class="org.yccheok.jstock.gui.WatchlistFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
每次轮换我都会得到以下 logcat
02-25 20:11:10.562: I/JStockFragmentActivity(6989): JStockFragmentActivity onCreate
02-25 20:11:10.597: I/WatchlistFragment(6989): WatchlistFragment onCreate
02-25 20:11:10.613: I/WatchlistFragment(6989): WatchlistFragment onActivityCreated
02-25 20:11:10.613: I/WatchlistFragment(6989): WatchlistFragment getTimeConsumingData
02-25 20:11:12.078: I/JStockFragmentActivity(6989): JStockFragmentActivity onCreate
02-25 20:11:12.109: I/WatchlistFragment(6989): WatchlistFragment onCreate
02-25 20:11:12.121: I/WatchlistFragment(6989): WatchlistFragment onActivityCreated
02-25 20:11:12.121: I/WatchlistFragment(6989): WatchlistFragment getTimeConsumingData
02-25 20:11:17.492: I/JStockFragmentActivity(6989): JStockFragmentActivity onCreate
02-25 20:11:17.519: I/WatchlistFragment(6989): WatchlistFragment onCreate
02-25 20:11:17.527: I/WatchlistFragment(6989): WatchlistFragment onActivityCreated
02-25 20:11:17.527: I/WatchlistFragment(6989): WatchlistFragment getTimeConsumingData
我想有没有应该在轮换期间一次又一次地setRetainInstance(true)
创造?WatchlistFragment