即使应用程序先前已关闭,它仍会打开

时间:2018-08-25 17:57:09

标签: android android-studio android-studio-3.0

我正在开发一个应用程序,而在手机上进行测试时却发现一个奇怪的现象:如果在启动屏幕运行时关闭该应用程序,则当上述活动结束时,即使该应用程序再次打开,即使我在另一个应用程序中。为什么会这样?

2 个答案:

答案 0 :(得分:0)

在SplashActivity中重写onDestroy方法:

 @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();
} 
}

答案 1 :(得分:0)

href

这是@Badran更新的代码