How to make an activity stop, rather then be destroyed, from the BACK key?

前端 未结 7 1315
心在旅途
心在旅途 2020-12-15 07:02

Right now an activity gets destroyed when the BACK key is pressed. How can I make it just stop ( i.e. keep all the variables, etc. alive ), rather then be destroyed?

相关标签:
7条回答
  • 2020-12-15 07:47

    First of all, sorry for not answering the question, cause, I still have no optimal answer for it. But, I really like when people start asking "what do you need this for". And, very rarely, the person who asked the question, really deserves this kind of question. I think not this time, but ok, this is not the issue...

    Anyway, I will try to point out why some of us are convinced that

    going from Activity A to Activity B(creating UI based on some data fetching) AND

    going back from B to A(destroying all the created UI and/or fetched data in B) is sometimes a bad concept. Better solution would be to keep the stack as it is, so using something like finish() in Activity B, but keeping the Activity B in Pause state, so later when calling it again from Activity A - it just goes in onResume = nothing recreated in UI, no additional data fetching. The bonus of course is a fast and responsive UI, and the difference is really if you have a more complicated UI layout.

    0 讨论(0)
提交回复
热议问题