У меня есть эта иерархия в моем андроид приложениеФрагмент перезагружается при изменении ориентации
активность содержит фрагмент FragmentGame, который содержит FragmentTypeOfGame;
активность >> FragmentGame >> FragmentTypeOfGame
Внутри FragmentGame, я получил таймер и другие метки, поэтому, когда я изменить ориентацию экрана, моя игра кажется перезапуском, но я могу проверить таймер продолжает работать (потому что это еще один поток, я думаю).
Итак, мой вопрос: как мне удастся сохранить весь экран без перезагрузки.
У меня уже были эти ссылки, но я не могу узнать, как его решить?
Why not use always android:configChanges="keyboardHidden|orientation"?
Forcing Android to not redraw activity on orientation change
Вот идет мой манифест:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="orientation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
<provider android:authorities="com.facebook.app.FacebookContentProviderXXXXXXXXX"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
</application>
Вот мой GameFragment OnCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Default components start
defaultComponentsStarting(inflater, container);
FourBlocksGameStyle f = new FourBlocksGameStyle();
frgManager.beginTransaction().replace(R.id.rl_root, f, "4blocks").addToBackStack(null).commit();
if (savedInstanceState != null) {
// Restore last state
actualGameTime = savedInstanceState.getDouble("actualGameTime");
}
handler = new Handler(Looper.getMainLooper());
mainRunnable = new Runnable() {
@Override
public void run() {
// Do something and reschedule ourself
if(GAMETIME <= actualGameTime){
cancelTimer();
setLabel(txtTimer, 0, GAMETIME/1000);
int countSum = Integer.parseInt(txtHits.getText().toString());
//Devolve pra main cuidar da finalização
Intent i = new Intent(ctx, MainActivity.class);
i.putExtra("CountSum", countSum);
startActivity(i);
}
else {
actualGameTime += GAMEDELAY;
double floatingTime = actualGameTime/1000;
setLabel(txtTimer, 0, floatingTime);
handler.postDelayed(this, GAMEDELAY);
}
}
};
return view;
}
и здесь моя MainActivity onCreate?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
final SharedPreferences prefs = getSharedPreferences("hitColor", MODE_PRIVATE);
int startHighscore = prefs.getInt("highScore", 0);
gameOver = new GameOverFragment();
highScoreGameOver = new HighScoreFragment();
game = new GameFragment();
Intent i = getIntent();
int countSum = i.getIntExtra("CountSum", -1);
if(countSum == -1) {
//Log.d("GSS", "Primeira entrada");
if(savedInstanceState == null) {
Log.d("GSS", "savedInstace null");
getFragmentManager()
.beginTransaction()
.replace(R.id.layout_root_view, game)
.addToBackStack(null)
.commit();
}
}
else {
Log.d("GSS", "COUNT: " + String.valueOf(countSum));
Log.d("GSS", "HIGH: " + String.valueOf(startHighscore));
finishGame(countSum, startHighscore, prefs);
}
}
Пожалуйста, проверьте этот вопрос. Он должен помочь вам: http://stackoverflow.com/questions/10126845/handle-screen-rotation-without-losing-data-android – aviundefined