android打开activity问题?
PHP中文网
PHP中文网 2017-04-17 17:42:27
0
3
429

我打开一个Activity列出一堆搜索结果之后按了退出键退出了,有没有办法我再打开这个Activity的时候别重新搜索,就打开刚刚退出的界面,别把onCreate重新执行一遍?

就是从这个Activity跳转到别的Activity,然后回来的效果那样

或者推出了就destroy了,没救了?

PHP中文网
PHP中文网

认证0级讲师

reply all(3)
PHPzhong

Pressing the exit key to exit the Activity will definitely cause the Activity to execute the onDestroy() method, so coming in again will definitely restart onCreate()onDestroy()方法的, 所以再进来肯定会重新onCreate()

想保留搜索结果我能想到2个方法

  1. 保存搜索的结果. 保存的方法比较多, 例如持久化搜索结果, 或者保存到一个静态对象里面, 或者把结果回传给启动它的Activity, 重新启动的时候再传回给它.

  2. 直接用Fragment显示搜索结果内容视图. 用Fragment点返回键时用hide()方法, 那么这个Fragment的实例就不会被回收, 重新进入搜索结果页面的时候再把这个show()

    I can think of 2 ways to keep the search results
    Save the search results. There are many ways to save, such as persisting the search results, or saving them in a static object, or passing the results back to the Activity that started it, and then passing them back when restarting Give it.

  3. #🎜🎜#Directly use Fragment to display the search result content view. Use the hide() method when using Fragment to click the return key, then the Fragment instance will not be recycled and the search results will be re-entered. Just put this show() when you create the page, but this method has requirements for the jump logic of the page.#🎜🎜##🎜🎜# #🎜🎜# #🎜🎜#It depends on the relationship between this search results page and other pages to determine which method to use#🎜🎜#
伊谢尔伦

I think you can set the launchMode of the Activity that displays the search results to singleTask, and then override the onNewIntent method of the Activity. However, the system will still kill the Activity at any time when the memory is tight. If you want to display the previous activity every time you open it, For search results, you can consider saving the search keywords in the database, and then use the last search keyword to request data every time you open it

Ty80

Before exiting the Activity, save the searched data into the Bundle, and then use the Bundle parameter in onCreate (which contains the data you saved before) to re-establish the View. . .
Well. . . That’s it in simple terms

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template