Android 仅在 lollipop 中应用程序每隔一秒崩溃一次

Android app crashing every second time in lollipop only

我是 Android 工作室的新手,我正在开发一个基于位置的应用程序。在这里,我需要在某些情况下关闭应用程序,所以我调用了 finish();函数并在 onDestroy()

中终止进程

问题:在 android 4.4(Kitkat) 中一切正常,但在 Lollipop 中崩溃(安装后第二次打开应用程序时崩溃)

public void onClick(){
    finish();
}

@Override                   //------------after the finish(); called---//
protected void onDestroy() {
    Process.killProcess(Process.myPid());
    super.onDestroy();
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}

@Override
protected void onPause() {
    super.onPause();
}    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    from = (EditText) findViewById(R.id.from);
    to = (EditText) findViewById(R.id.to);
    go = (Button) findViewById(R.id.go);
    resulted = (TextView) findViewById(R.id.result);
    time = (TextView) findViewById(R.id.time1);
    button = (Button) findViewById(R.id.button);


    android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

     audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);


    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    pb = (ProgressBar) findViewById(R.id.progressBar1);
    pb.setVisibility(View.INVISIBLE);
    locationManager =            (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    go.setOnClickListener(new View.OnClickListener() {
                              public void onClick(View v) {

                                  str_from1=from.getText().toString();
                                  str_to1=to.getText().toString();

                                             str_from1 = str_from1.replaceAll("[^\w]+", "+");
                                              str_to1 = str_to1.replaceAll("[^\w]+", "+");
                                              new JSONTask().execute("https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + str_from1 + "&destinations=" + str_to1 + "&mode=driving&language=fr-FR&key=API KEY");
                              }
                          }
    );
}

好吧,您不需要终止进程。调用 finish() 应该足够了;它将在其自然生命周期内销毁当前 activity,如果它是最后一个 activity,应用程序将有效退出。

有关为什么不应该朝那个方向发展的更多讨论: Is quitting an application frowned upon?

此致!