简体   繁体   English

Android活动需要恢复或不重新显示

[英]Android activity need to resume or display not to re-create

I am developing an Android application. 我正在开发一个Android应用程序。 There is a activity form. 有一个活动表格。 The user is going to fill out the form. 用户将要填写表格。 To fill out the form the user need to go to other forms and after getting back the form is reloading. 要填写表格,用户需要转到其他表格,并且在取回表格后重新加载。

I want the form to resume, not to re-create. 我希望表格恢复,而不是重新创建。

may help you also: http://developer.android.com/reference/android/app/Activity.html 可能还会帮助您: http : //developer.android.com/reference/android/app/Activity.html

@Override
public void onResume(){

    // put your code here...

}

This is correct behavior, according to the documentation . 根据文档 ,这是正确的行为。 You will want to override onSaveInstanceState() to save what the Activity currently looks like, and then properly recreate it in onCreate() . 您将需要覆盖onSaveInstanceState()来保存Activity当前的外观,然后在onCreate()正确地重新创建它。

Do this well, and your app will be rock solid. 做到这一点,您的应用将如虎添翼。 Some folks will generally recommend certain lazy short cuts which are not recommended and will in some cases lead to very unstable applications. 有些人通常会建议您不要使用某些懒惰的捷径,这在某些情况下会导致应用程序非常不稳定。

There are two ways you can do this: 有两种方法可以执行此操作:

  1. Use startActivityForResult() , It will start another activity (means new forms etc) and then you will return back to original activity without any recreation of views and elements. 使用startActivityForResult() ,它将启动另一个活动(表示新表单等),然后您将返回到原始活动,而无需重新创建视图和元素。 You can do your stuff in onActivityResult() See How to manage `startActivityForResult` on Android? 您可以在onActivityResult()完成您的工作,请参阅如何在Android上管理`startActivityForResult`?

  2. Or save your data in onSaveInstanceState() before leaving to activity and on returning back reload data. 或在继续进行活动并返回重新加载数据之前将数据保存在onSaveInstanceState() see this thread for further help onSaveInstanceState () and onRestoreInstanceState () 请参阅此线程以获取更多帮助onSaveInstanceState()和onRestoreInstanceState()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM