我在setRetainInstance(true)
上使用SherlockListFragment
,并期望它在屏幕旋转期间不会破坏并重新创建。但事实并非如此。每次我执行轮换时它仍然会调用onActivityCreated
。
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();
}
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();
}
<?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
?
答案 0 :(得分:0)
根据the specs:
这是正确的
onDestroy()
将不会被调用(但onDetach()
仍然会被调用,因为片段正在与其当前活动分离。)onCreate(Bundle)
将不会被调用,因为片段没有被重新创建。onAttach(Activity)
和onActivityCreated(Bundle)
仍会被称为。
答案 1 :(得分:0)
是的,它会调用onActivityCreated()
,但不会调用onCreate()
和onCreateView()
。因此,请将您不希望再次调用的代码移至onCreate()
。