Finishing all activities started before the activity

前端 未结 3 1551
难免孤独
难免孤独 2021-02-09 15:16

I want to finish all the activities which are running in the application means want to remove all the parent activities from stack.

I want to implement logout functional

3条回答
  •  一个人的身影
    2021-02-09 16:12

    What you might be looking for is FLAG_ACTIVITY_CLEAR_TOP intent flag:

    If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

    Intent i = new Intent(..);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    

    and then start Activity with this intent.

    See more on tasks and back stack in documentation: Tasks and Back stack.

    However to implement login/logout to application (if it is not communicating with online service) you might use SharedPreferences. This way on application start you could check if user is logged in (eg. some flag turned on in preferences) and upon application exit (eg. by button) you could clear this flag.

    Killing/removing Activities should be left to the system. Per system design it is Android OS that is responsible for application lifetime.

    For example check the Password Safe application sources. It needs password every time you open fresh instance of application.

提交回复
热议问题