Update views in one Activity from another

2020-05-06 07:52发布

I am trying to emulate a software keyboard using an Activity with Theme.Dialog. The keyboard activity covers only part of the screen. The underlying activity is still active and can be controlled directly. I would like the entered text to be displayed in the underlying Activity as it is typed.

Is there a way I can do this? I was thinking of throwing a reference of my EditText into the Intent Bundle, but that does not seem correct.

edit: someone answered to make the EditText static, but then deleted the response. Could they add it back in case this turns out to be the correct response?

3条回答
SAY GOODBYE
2楼-- · 2020-05-06 08:21

I'm answering my own question because the person who supplied the original answer deleted it, and has not reposted it.

The best solution I've found so far it to create a static field in the software keyboard activity which refers to the EditText being manipulated. The calling Activity then updates this static field before launching the intent.

This works seamlessly, and I've had no problems with the calling Activity getting blasted off the stack.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-05-06 08:23

It will not work.. your Activity will be in paused state.. you try to invoke something there it sure will throw an Exception.. don't try it.. try onActivityResult() instead...

:Edited

well then try this..

get the Activity instance (i mean the activity which has edit text) then

if(!yourActviity.isFinishing())
{

((EditText)yourActviity.findViewById(R.id.editText)).setText("text");
}
查看更多
ゆ 、 Hurt°
4楼-- · 2020-05-06 08:33

if u are using custom dialog u can acess the variables in main activity using its mainactivity context.for that u need to cast the context in mainactivity type

查看更多
登录 后发表回答