简体   繁体   English

Android:返回上一个活动时,文本字段显示为空

[英]Android : Text Fields are displaying empty when returning to previous activity

I have a problem with back button functionality in an Activity of Android. 我在Android的Activity对后退按钮功能有疑问。 The first Activity having the Spinner to select one item from the Spinner's list and second one is the text field. 第一个具有Spinner Activity可以从Spinner's列表中选择一个项目,第二个活动是文本字段。 I implemented search functionality using the Spinner and text field. 我使用Spinner和文本字段实现了搜索功能。 The results are displaying fine as a ListView . 结果显示为ListView很好。

Here my Problem is: 这是我的问题是:

While returning to the first Activity , the Spinner and text field are showing empty in the Activity . 返回第一个ActivitySpinner和文本字段在Activity中显示为空。 It should show the previous searched results. 它应该显示以前的搜索结果。

Help me with the sample code/ links. 使用示例代码/链接帮助我。

Its normal. 这是正常的。 When your first Activity goes to back ground its finished by System itself. 当您的第一个活动回退时,由系统本身完成。 So make sure to save your data in some place and in Activitie's onCreate() and onRestart() method reload the data to TextView and spinner.. 因此,请确保将数据保存在某个位置,并在Activitie的onCreate()onRestart()方法中将数据重新加载到TextView和Spinner。

Edits: Create a Data class and Store your search results in String[] array or a String or how ever you like it. 编辑:创建一个Data类,然后将搜索结果存储在String []数组或String或您希望使用的方式中。 and make the class a singlton class. 使班级成为辛格尔顿班。 and when you come back to this screen fetch those data's and set Text of TextView and adapter for Spinner.. 当您返回此屏幕时,获取这些数据并设置TextView的文本和Spinner的适配器。

Shafi yes, i am calling the back function using Intent as Intent i=new Intent (presentClass.this, previousClass.class); Shafi是的,我正在使用Intent作为Intent调用back函数,我= new Intent(presentClass.this,previousClass.class);

Don't do this. 不要这样 Because the Activity stack will become like loop with same Activities started again n again. 因为Activity堆栈将变得像循环,而相同的Activity再次从n开始。 Instead just finish presentClass .. it will come back to previousClass 而是只需完成presentClass ..它将返回到previousClass

除了@ntc帖子外,还可以查看Activity中的onSaveInstanceState(Bundle outState)onRestoreInstanceState(Bundle state)方法。

Dont create a new intent. 不要创建新的意图。 You just need to call finish() from your second Activity to handle back event and move back to your first activity. 您只需要从第二个Activity调用finish()来处理后退事件并移回您的第一个Activity。

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

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