How to kill process by clicking back button in Android?

柔情痞子 提交于 2019-12-08 01:26:57

问题


I have made an application in android .It is an application related to timer.Now i have tried a lot to put controll on back button of android device that when it is pressed the process should be killed and directly displays main menu...Please help me...Thanking you in advance..!


回答1:


Try this code

@Override
public void onBackPressed() {
   yourActivity.this.finish;
}



回答2:


override onBackPressed() in your activity and add your necessary codes there

onBackPressed

Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.




回答3:


you can do like this onclick of back button.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
         finish();// call finish() on click of back button
    }
    return super.onKeyDown(keyCode, event);
}

for example : When you moving from A activity to B activity using startActivity(new intent(...)); don't finish or kill A activity, It ll be in stack.So when you click on back button in B activity you can go back to A activity, which is already in stack.

when you want to go back to main menu call finish() on every activity when your moving to next activity.

for example : When you moving from A activity to B activity using startActivity(new intent(...)); call finish() to kill A activity.So when you click on back button in B activity you can go back to Main Menu coz every activity ll be killed.

snippet is here :

startActivity(new intent(A.this, B.class));
finish(); // when you click back button on B activity, directly you can go to main menu

Updated : Other way to kill the app using below code on back button pressed. But not recommended

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
         android.os.Process.killProcess(android.os.Process.myPid());// Kill the app on click of back button.
    }
    return super.onKeyDown(keyCode, event);
}

hope this might help you to understand the concept.




回答4:


Just clear the activity history so that your application gets closed.And call finsh(); afterstartActivity();

  <activity
        android:name="com.example.abcd.Your_Acitivty"
        android:noHistory="true">
  </activity>

Happy Coding !




回答5:


well jimmy you can use this code to go to main screen of your device-

    finish();
    moveTaskToBack(true);
     System.exit(0);    

finish() method closes current Activity only.

moveTasktoBack will hide your app and You can also kill an application quickly via android.os.Process.killProcess(android.os.Process.myPid()) if you want.




回答6:


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}



回答7:


In the Manifest, you could try adding this

<activity
    android:name=".MainActivity"
    android:finishOnCloseSystemDialogs="true"
</activity>

I was having a similar issue, and this solved it for me.




回答8:


I solved this by override onBackPress() and onStop()

@Override
protected void onStop() {
    super.onStop();
    System.exit(0); //kill the app

}

@Override
public void onBackPressed() { //here I capture the event onBackPress 
    super.onBackPressed(); 
    onStop(); //call onStop
}


来源:https://stackoverflow.com/questions/14109794/how-to-kill-process-by-clicking-back-button-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!