Ahoj, prosím Vás mám jednoduchú aplikáciu, ktorá ale stráca data keď
sa zmení orientácia na telefónne, je to známi problém ktorí sa dá
riešiť týmto spôsobom "":https://developer.android.com/…fecycle.html#…
Ja ale absolútne nechápem ako by som to mal aplikovať do kódu svojej
aplikácie, a to sa s tým trápim už relatívne dlho. Kód by mal vyzerať
ukážkovo nejako takto:
static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";
int mCurrentScore = 0;
int mCurrentLevel = 0;
@Override
publicvoid onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
publicvoid onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
// display data again
}
Kód mojej appky je tu:
package com.example.android.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
publicclass MainActivity extends AppCompatActivity {
int ScoreOfTeamA = 0;
int ScoreOfTeamB = 0;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* Increase the score for Team A by 1 point.
*/publicvoid addOneForTeamA(View v) {
ScoreOfTeamA = ScoreOfTeamA + 1;
displayForTeamA(ScoreOfTeamA);
}
/**
* Increase the score for Team A by 2 points.
*/publicvoid addTwoForTeamA(View v) {
ScoreOfTeamA = ScoreOfTeamA + 2;
displayForTeamA(ScoreOfTeamA);
}
/**
* Increase the score for Team A by 3 points.
*/publicvoid addThreeForTeamA(View v) {
ScoreOfTeamA = ScoreOfTeamA + 3;
displayForTeamA(ScoreOfTeamA);
}
/**
* Increase the score for Team B by 1 point.
*/publicvoid addOneForTeamB(View v) {
ScoreOfTeamB = ScoreOfTeamB + 1;
displayForTeamB(ScoreOfTeamB);
}
/**
* Increase the score for Team B by 2 points.
*/publicvoid addTwoForTeamB(View v) {
ScoreOfTeamB = ScoreOfTeamB + 2;
displayForTeamB(ScoreOfTeamB);
}
/**
* Increase the score for Team B by 3 points.
*/publicvoid addThreeForTeamB(View v) {
ScoreOfTeamB = ScoreOfTeamB + 3;
displayForTeamB(ScoreOfTeamB);
}
/**
* Displays the given score for Team A.
*/publicvoid displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
/**
* Displays the given score for Team B.
*/publicvoid displayForTeamB(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_b_score);
scoreView.setText(String.valueOf(score));
}
publicvoid Reset(View v) {
ScoreOfTeamA = 0;
displayForTeamA(ScoreOfTeamA);
ScoreOfTeamB = 0;
displayForTeamB(ScoreOfTeamB);
}
}
Ďakujem veľmi pekne keby mi niekto napísal aspoň nejakú malú ukážku,
ako by mal ten môj kód vyzerať, ďakujem.
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.