私は次の非常に単純なフラグメントを持っています。
UiFragment.java
package com.example.test;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;
/**
* This is a fragment showing UI that will be updated from work done
* in the retained fragment.
*/
public class UiFragment extends SherlockFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.i("CHEOK", "UiFragment onActivityCreated");
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i("CHEOK", "UiFragment onCreateView");
View v = inflater.inflate(R.layout.uifragment, container, false);
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("CHEOK", "UiFragment onCreate");
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
Log.i("CHEOK", "--> UiFragment able get savedInstanceState's data : " + savedInstanceState.getInt("value"));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("value", 111222);
}
}
uifragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="fragment_retain_instance_msg" />
</LinearLayout>
Javaコードを介してFragmentActivityにアタッチしたかどうかがわかります。
MainActivity.java
package com.example.test;
import android.os.Bundle;
import android.util.Log;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class MainActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("CHEOK", "MainActivity onCreate");
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
Log.i("CHEOK", "--> MainActivity able get savedInstanceState's data : " + savedInstanceState.getInt("value"));
}
// ???
//setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(android.R.id.content,
new UiFragment()).commit();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("value", 888999);
}
}
ローテーションを実行するたびFragment
にsavedInstanceState
、onCreate
02-26 00:36:48.777: I/CHEOK(17740): MainActivity onCreate
02-26 00:36:48.812: I/CHEOK(17740): UiFragment onCreate
02-26 00:36:49.031: I/CHEOK(17740): UiFragment onCreateView
02-26 00:36:49.035: I/CHEOK(17740): UiFragment onActivityCreated
[Rotation happens here]
02-26 00:37:37.472: I/CHEOK(17740): MainActivity onCreate
02-26 00:37:37.472: I/CHEOK(17740): UiFragment onCreate
02-26 00:37:37.472: I/CHEOK(17740): --> UiFragment able get savedInstanceState's data : 111222
02-26 00:37:37.472: I/CHEOK(17740): --> MainActivity able get savedInstanceState's data : 888999
02-26 00:37:37.511: I/CHEOK(17740): UiFragment onCreateView
02-26 00:37:37.511: I/CHEOK(17740): UiFragment onActivityCreated
ただし、MainActivity
'sのコードを置き換えた場合onCreate
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(android.R.id.content,
new UiFragment()).commit();
}
XMLによるフラグメント作成
setContentView(R.layout.activity_main);
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment class="com.example.test.UiFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ローテーションを実行すると、次のログが表示されます。
02-26 00:39:23.437: I/CHEOK(18055): MainActivity onCreate
02-26 00:39:23.734: I/CHEOK(18055): UiFragment onCreate
02-26 00:39:23.734: I/CHEOK(18055): UiFragment onCreateView
02-26 00:39:23.754: I/CHEOK(18055): UiFragment onActivityCreated
[Rotation happens here]
02-26 00:39:27.703: I/CHEOK(18055): MainActivity onCreate
02-26 00:39:27.703: I/CHEOK(18055): --> MainActivity able get savedInstanceState's data : 888999
02-26 00:39:27.746: I/CHEOK(18055): UiFragment onCreate
02-26 00:39:27.746: I/CHEOK(18055): UiFragment onCreateView
02-26 00:39:27.750: I/CHEOK(18055): UiFragment onActivityCreated
Fragment
なぜそのような行動の違いがあるのですか?XMLで作成されている場合、ローテーション中にnull以外のsavedInstanceStateバンドルを受信できないのはなぜですか?Fragment