Skip to content

Instantly share code, notes, and snippets.

@SyncHack
Created April 1, 2012 06:49
Show Gist options
  • Save SyncHack/2272060 to your computer and use it in GitHub Desktop.
Save SyncHack/2272060 to your computer and use it in GitHub Desktop.
// SherlockFragmentActivityになってるけどActivityでも動作するよ
// MainActivityはHoneycombGalleryのまんま流用
public class MainActivity extends SherlockFragmentActivity implements
TitlesFragment.OnItemSelectedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
//追加箇所:RecreateActivityから呼び出された時の処理をしている
Bundle recreateState = RecreateActivity.recreateBundle(getIntent());
if (recreateState != null) {
// オリジナルのコピペ。手抜きすぐるw
if (recreateState.getInt("theme", -1) != -1) {
mThemeId = recreateState.getInt("theme");
this.setTheme(mThemeId);
}
mTitlesHidden = recreateState.getBoolean("titlesHidden");
}
} else {
// オリジナル
if (savedInstanceState.getInt("theme", -1) != -1) {
mThemeId = savedInstanceState.getInt("theme");
this.setTheme(mThemeId);
}
mTitlesHidden = savedInstanceState.getBoolean("titlesHidden");
}
// super.onCreateより上でsetThemeはよもや常識のバッドノウハウw
// もうやだ
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 以下略
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// 変更箇所:苦肉の策。onSaveInstanceStateで保存されるパラメータを共用
addSaveInstanceState(outState);
}
// 追加箇所:パラメータの内容は素のままでOK
private void addSaveInstanceState(Bundle outState) {
outState.putInt("theme", mThemeId);
outState.putBoolean("titlesHidden", mTitlesHidden);
}
// 追加箇所:むしゃくしゃしてたのでOverrideしたw
@Override
public void recreate() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Android 3.x以上なら通常処理。念を入れるならrefrect処理も。
super.recreate();
} else {
Bundle outState = new Bundle();
addSaveInstanceState(outState);
startActivity(RecreateActivity.recreateIntent(
getApplicationContext(),
getClass().getName(),
outState));
overridePendingTransition(0, 0);
finish();
overridePendingTransition(0, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment