I reset the chronometer value when you change the orientation, has written is true, but does not help, ask for help, what went wrong
#Override
protected void onSaveInstanceState(Bundle outState) {
timer = SystemClock.elapsedRealtime() - chronometer.getBase();
outState.putLong("timerValue", timer);
super.onSaveInstanceState(outState);
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_layout);
chronometer = (Chronometer) findViewById(R.id.chronometer);
chronometer.setBase(SystemClock.elapsedRealtime());
if (savedInstanceState != null) {
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
public void onChronometerTick(Chronometer chronometer) {
timer = savedInstanceState.getLong("timerValue");
chronometer.setText(DateFormat.format("mm:ss", timer));
}
});
chronometer.start();
} else {
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
public void onChronometerTick(Chronometer chronometer) {
timer = SystemClock.elapsedRealtime() - chronometer.getBase();
chronometer.setText(DateFormat.format("mm:ss", timer));
}
});
chronometer.start();
}
}
Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).
See:
How to contain value of android chronometer with change in orientation
Related
I have a CountDownTimer in one activity that I need to cancel in a different activity. The problem I'm having is that the CountDownTimer is not getting cancelled even when I make a call to the endTestTimer() method in another activity. Here is the code for the CountDownTimer:
private long testMillisLeft, questionMillisLeft;
private static CountDownTimer testTimer;
private void startTestTimer()
{
long startTime = DataHolder.getTestTime()*1000; //getTestTime() is the seconds
testTimer = new CountDownTimer(startTime, 1000) {
#Override
public void onTick(long millisUntilFinished) {
testMillisLeft = millisUntilFinished;
updateTestTimeLeft();
}
#Override
public void onFinish() {
Intent intent = new Intent(MainActivity4.this, MainActivity5.class);
startActivity(intent);
}
}.start();
}
public static void endTestTimer()
{
if(testTimer != null)
{
testTimer.cancel();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
testTimeLeft = (TextView) findViewById(R.id.testTimerTextView);
mathProblem = (TextView) findViewById(R.id.mathProblemTextView);
testTimer = null;
startTestTimer();
}
And here is how I am trying to cancel the same timer in another activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainActivity4.endTestTimer();
}
A comment on this related post says that the static CountDownTimer creates memory leak. If that's the case, how can I call the endTestTimer() method in another activity and have it cancel the timer? Alternatively, how can I access the testTimer CountDownTimer directly and call .cancel() on it in another activity if it can't be static?
Looking into Android lifecycles led me to this post and this post, both of which I found very helpful. To my understanding, when the user hits the back button, the onDestroy() method is called for the currently-displayed activity, and then the onCreate() method is called for the activity to be displayed. So, to cancel the timers on MainActivity4 (the current activity in my case), I added this code to its class file:
#Override
protected void onDestroy() {
testTimer.cancel();
questionTimer.cancel();
super.onDestroy();
}
Now, when the user backs out of MainActivity4 (regardless of what other activity that takes them to) and onDestroy() is called automatically, both timers are cancelled and then MainActivity4 is destroyed. This seems to work fine for me, but I'm not sure if there are any downsides to doing it this way, so please let me know if there are.
When I'm leaving my app, my counter gets reset.
I know that I need to use the onPause and onResume functions. However, I don't know how to write it in the code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button b1 = findViewById(R.id.b1);
eggcounter = 100;
final ImageButton ImgButton = findViewById(R.id.eggBtn);
ImgButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
eggcounter = eggcounter - 1;
updateEgg();
if (eggcounter < 80) {
ImgButton.setImageResource(R.drawable.egg_2);
if (eggcounter <60){
ImgButton.setImageResource(R.drawable.egg_3);
if (eggcounter <40) {
ImgButton.setImageResource(R.drawable.egg_4);
If you want to save your application state, you can use Shared Preferences. Example :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// You can call to onResume() or on Pause() in onCreate();
onResume();
onPause();
}
#Override
public void onResume() {
super.onResume();
// After receiving this call you will usually receive a following call
// to onStop() and return to your activity 's onResume()
}
#Override
public void onPause() {
super.onPause();
// When you receive a call, your activity is going into the background
// (to onPause) but your activity can be killed to reclaim resources
// (if there are not enough resources to start the new activity)
}
so if you want to save your instance application state, you can save to Shared Preferences, SQLite and so on. Call it in onPause() or onStop() and reclaim this data in onResume()
I'm trying to integrate Interstitial Ad in splash screen activity
by using this tutorial
.. but the ad not loading.
can anyone tell me where is the problem please?
thanks in advance
here is my code :
public class SplashScreenActivity extends Activity {
private InterstitialAd mInterstitialAd;
private Timer waitTimer;
private boolean interstitialCanceled;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
mInterstitialAd.setAdListener(new AdListener()
{
#Override
public void onAdLoaded() {
if (!interstitialCanceled) {
waitTimer.cancel();
mInterstitialAd.show();
}
}
#Override
public void onAdFailedToLoad(int errorCode) {
startHomeMain();
}
});
waitTimer = new Timer();
waitTimer.schedule(new TimerTask() {
#Override
public void run() {
interstitialCanceled = true;
SplashScreenActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
startHomeMain();
}
});
}
}, 5000);
} // end of onCreate implementation.
private void startHomeMain() {
Intent intent = new Intent(this, HomeMain.class);
startActivity(intent);
finish();
}
#Override
public void onPause() {
waitTimer.cancel();
interstitialCanceled = true;
super.onPause();
}
#Override
public void onResume() {
super.onResume();
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else if (interstitialCanceled) {
startHomeMain();
}
}
}
I'm trying to integrate Interstitial Ad in splash screen activity
by using this tutorial
.. but the ad not loading.
can anyone tell me where is the problem please?
thanks in advance
Google ads takes minimum 6 seconds to Load , and in your code you have set timer of 5 seconds , so thats why your Splash screen activity finished but ad not shown
try to set timer of greater than 6 seconds , and display add on destroy method of activity.
Remove timer and add progress dialogue and in onAdLoaded Dismiss dialogue and show ad as well as Dismiss dialogue in onFailedtoLoad
I can successfully cancel the Countdown Timer before it finishes when the activity is paused. However, the media player implemented in the OnTick method of the canceled timer doesn't stop for 5-8 seconds. What would be the correct way to disable the sound of the media player in the CountDown Timer when the activity is paused?
private Button soundOn;
private Button soundOff;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_one);
SharedPreferences savedSoundToggle = getSharedPreferences(PREFS_SOUNDTOGGLE, 0);
final SharedPreferences.Editor soundEditor = savedSoundToggle.edit();
nSound = savedSoundToggle.getInt("SoundOnorOff", 0);
if(nSound == 1){
soundOff.setEnabled(false);
soundOff.setVisibility(View.INVISIBLE);
soundOn.setEnabled(true);
soundOn.setVisibility(View.VISIBLE);
}
else if( nSound == 0){
soundOn.setEnabled(false);
soundOn.setVisibility(View.INVISIBLE);
soundOff.setEnabled(true);
soundOff.setVisibility(View.VISIBLE);
}
}
public void OneGame(){
final MediaPlayer intro = MediaPlayer.create(getApplicationContext(), R.raw.minicycleaudio);
final MediaPlayer go = MediaPlayer.create(getApplicationContext(), R.raw.flashgoaudio);
getReadyTimer = new CountDownTimer(6000, 1000) {
public void onTick(long millisUntilFinished) {
getReadyTimeText.setText("" + millisUntilFinished / 1000);
if(soundOn.isEnabled()){
intro.start();
}
if(soundOff.isEnabled()){
if(intro.isPlaying()){
intro.pause();
}
}
}
public void onFinish() {
if(intro.isPlaying()) {
intro.stop();
intro.release();
}
if(soundOn.isEnabled()) {
go.start();
}
#Override
public void onPause() {
super.onPause();
getReadyTimer.cancel();
}
}
I'm almost 100% positive that the Countdown timer becomes canceled when the activity is paused because the media player in the onFinish() function is never called. Why does the media player in the onTick method keep playing?
I figured it out: To cancel a media player in a CountDown Timer, when the activity is paused... you must declare the MediaPlayer as static, not final.
static MediaPlayer intro;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_one);
getReadyTimeText = (TextView) findViewById(R.id.levelonetimerText);
public void OneGame(){
intro = MediaPlayer.create(getApplicationContext(), R.raw.minicycleaudio);
}
I have a CharSequence which displays a sequence of text after each imageview click however the CharSequence seems to restart if the orientation is changed mid sequence.
Does anyone know how this can be resolved?
On an orientation change the activity is restarted, and the inCreate() is called again. You have to take that in consideration.
A small example of how to store and retrieve a value:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
isStarted = savedInstanceState.getBoolean("isStarted");
}
}
#Override
protected void onResume() {
isStarted = true;
super.onResume();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("isStarted", isStarted);
super.onSaveInstanceState(outState);
}
For more information and methods: Saving Android Activity state using Save Instance State
using android:configChanges="orientation|keyboardHidden|screenSize"> resolved the issue