Close application and launch home screen on Android

后端 未结 21 2201
我在风中等你
我在风中等你 2020-11-22 07:33

I have two different activities. The first launches the second one. In the second activity, I call System.exit(0) in order to force the application to close, bu

21条回答
  •  无人共我
    2020-11-22 08:18

    The easiest way for achieving this is given below (without affecting Android's native memory management. There is no process killing involved).

    1. Launch an activity using this Intent:

      Intent intent = new Intent(this, FinActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(intent);
      finish();
      
    2. In the target activity FinActivity.class, call finish() in onCreate.

    Steps Explained:

    1. You create an intent that erases all other activities (FLAG_ACTIVITY_CLEAR_TOP) and delete the current activity.

    2. The activity destroys itself. An alternative is that you can make an splash screen in finActivity. This is optional.

提交回复
热议问题