How to close activity and go back to previous activity in android

后端 未结 18 1852
孤独总比滥情好
孤独总比滥情好 2020-12-07 08:19

I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:

Intent intent = new Intent(this, SettingsAc         


        
18条回答
  •  心在旅途
    2020-12-07 08:46

    We encountered a very similar situation.

    Activity 1 (Opening) -> Activity 2 (Preview) -> Activity 3 (Detail)

    Incorrect "on back press" Response

    • Device back press on Activity 3 will also close Activity 2.

    I have checked all answers posted above and none of them worked. Java syntax for transition between Activity 2 and Activity 3 was reviewed to be correct.

    Fresh from coding on calling out a 3rd party app. by an Activity. We decided to investigate the configuration angle - eventually enabling us to identify the root cause of the problem.

    Scope: Configuration of Activity 2 (caller).

    Root Cause:

    android:launchMode="singleInstance"
    

    Solution:

    android:launchMode="singleTask"
    

    Apparently on this "on back press" issue singleInstance considers invoked Activities in one instance with the calling Activity, whereas singleTask will allow for invoked Activities having their own identity enough for the intended on back press to function to work as it should to.

提交回复
热议问题