Приложение открывается в любом случае, даже если оно было закрыто ранее - PullRequest
0 голосов
/ 25 августа 2018

Я работаю над приложением, и во время тестирования на моем телефоне было замечено что-то странное: если я закрываю приложение во время работы заставки, когда время действия, о котором говорилось выше, приложение снова открывается, даже если Я в другом приложении. Почему это происходит?

Ответы [ 2 ]

0 голосов
/ 27 августа 2018
package com.example.arlet.storemaps;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;

import java.util.Timer;
import java.util.TimerTask;

public class SplashScreenActivity extends AppCompatActivity {
//duration of splash screen in miliseconds
long delay = 6000;
private Timer timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_splash_screen);

    Timer RunSplash = new Timer();

    TimerTask ShowSplash = new TimerTask() {
        @Override
        public void run() {
            //finishing splash screen
            finish();

            //starting main activity
            Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
            startActivity(intent);
        }
    };
    RunSplash.schedule(ShowSplash, delay);
}

@Override
protected void onDestroy(){
    timer.cancel();
    timer.purge();
    super.onDestroy();
}
}

Вот обновленный код @ Badran

0 голосов
/ 25 августа 2018

Переопределите метод onDestroy в SplashActivity:

 @Override
    protected void onDestroy() {
        //remove the handler or thread or etc... that is opening the another activity
        //call timer.cancel()
        //call timer.purge ()
        super.onDestroy();
    }

Итак, ваш код будет теперь:

package com.example.arlet.storemaps;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;

import java.util.Timer;
import java.util.TimerTask;

public class SplashScreenActivity extends AppCompatActivity {
//duration of splash screen in miliseconds
long delay = 6000;
private Timer RunSplash;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_splash_screen);

    RunSplash = new Timer();

    TimerTask ShowSplash = new TimerTask() {
        @Override
        public void run() {
            //finishing splash screen
            finish();

            //starting main activity
            Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
            startActivity(intent);
        }
    };
    RunSplash.schedule(ShowSplash, delay);
}

@Override
protected void onDestroy(){
    if(RunSplash != null){
    RunSplash.cancel();
    RunSplash.purge();
    }
    super.onDestroy();
}


@Override
protected void onPause() {
    if(RunSplash != null){
        RunSplash.cancel();
        RunSplash.purge();
    }
    super.onPause();
} 
}
...