我是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");
}
}
);
}
答案 0 :(得分:0)
嗯,你不应该杀死这个过程。调用finish()就足够了;它将通过其自然生命周期破坏当前的活动,如果它是最后的活动,应用程序将有效地退出。
有关为什么不应该朝这个方向前进的更多讨论: Is quitting an application frowned upon?
问候!