简体   繁体   English

在按下返回按钮后再次创建活动时,EditText返回相同的值

[英]EditText returning same value when activity is created again after the back button is pressed

That's my first post obviously here...okay so my problem is as follows: @android: i have a TextEdit that a user enters in text, i get the text by getText() and it all works fine, but when i press the back button , and again come back to the same activity of the TextEdit and try to get the text again, it returns and empty string"" eventhough a different text is shown in the TextEdit view. 那显然是我的第一篇文章...好吧,所以我的问题如下:@android:我有一个TextEdit,用户在其中输入文本,我通过getText()得到了文本,并且一切正常,但是当我按后退按钮,然后再次返回到TextEdit的相同活动,并尝试再次获取文本,即使在TextEdit视图中显示了不同的文本,它也会返回并显示空字符串“”。 Could it be that the TextEdit is still "relating or corresponding" to the old closed activity? 是不是TextEdit仍与旧的已关闭活动“相关或相对应”? Any ideas ? 有任何想法吗 ?

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.messaging_screen2);

    editText = (EditText) findViewById(R.id.msgText);
    Button send = (Button)findViewById(R.id.Go);
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           textToSend=editText.getText().toString();  
        }
    });

      }

Then in an AsynckTask i send the textToSend, 然后在AsynckTask中,我发送textToSend,

@Override
protected String doInBackground(Void... arg0) {

        while(!done)
        {
            if(isCancelled())
            {
                done=true;
                break;
            }

            if(text.contains(Protocols.RESPONSE_OK))
               {
        System.out.println("getting message from edit text: "+textToSend);          
                publishProgress(textToSend);     
               }
        }           
        return null;            
    }

Now the when i can send msgs normally but when i press the back button and then start the same activity again, the FIRST msg sent is exactly the old one( upon pressing the send button for the first time ). 现在,当我可以正常发送消息时,但是当我按下后退按钮然后再次开始相同的活动时,发送的第一个消息恰好是旧消息(第一次按下发送按钮时)。 when i press the send button again, the shown text in the EditText is sent properly. 当我再次按下发送按钮时,EditText中显示的文本将正确发送。 so any help ? 有什么帮助吗? Thanks. 谢谢。 After some debugging i noticed that the value of textToSend in send.Onclick() method is not the same as the one in the doInBackGround of the AsynckTask after creating the activity again. 经过一些调试后,我注意到send.Onclick()方法中textToSend的值与AsynckTask的doInBackGround中的值再次不同,再次创建了该活动。 This happens on the first click of send, after witch everything works fine. 在女巫一切正常之后,第一次单击发送时就会发生这种情况。

Its weird, Paste your code, 很奇怪,粘贴您的代码,

Try set edittext.setText("test") in button's onClick() method. 尝试在按钮的onClick()方法中设置edittext.setText(“ test”)。 And check what return edittext.getText() when u back. 并检查当您返回时返回return text.getText()的内容。

To change Activity use you this syntax? 要更改活动,请使用以下语法?

Intent direction = new Intent(Class.this, DirectionClass.class);   
startActivity(direction);  

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

相关问题 在“后退”按钮之后再次创建活动时,可打包数据不可用 - Parcelable data unavailable when an Activity is created again after back button 在启动应用程序后,按下后​​退按钮会显示相同的活动 - On back button pressed same activity appears when app is launched 按下后退按钮时活动布局的问题 - problems with activity layout when back button is pressed 按下“主页”按钮后,从点击图标应用程序再次启动我的活动后,我的活动不会重新加载 - After Home button pressed, my Activity not reload when launch it again from click icon app 按下返回按钮后媒体播放器崩溃,然后再次打开应用 - media player crash after back button is pressed and open app again 在android中按下导航后退按钮时保留edittext数据 - preserve edittext data when navigation back button pressed in android 按下后退按钮后,按原样恢复原始活动(如主页按钮) - Restoring original activity as it is (like home button) when back button pressed 按下后退按钮后如何恢复活动状态? - How to restore state of activity after back button has been pressed? 按下“ BACK”(返回)按钮时是否返回正常活动? - Returning to the proper activity when pressing the BACK button? 按下按钮时新建EditText - New EditText when button pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM