I have two activities A and B, and from activity A, I click on a button that opens a dialog box which contains a form consisting of two edit text fields and a button(the button in the dialog box is used to start activity B). So, my question is: how do I pass a string from activity B to activity A, but without closing the dialog box(the string will be used to fill one of the two edit text fields).
-
Please can you clarify your question? I have a doubt about what your activity B is? – Amokrane Chentir Jun 18 '11 at 11:58
-
after click on button in activity A it will open dialog box in same activity? Also you want to pass the string from activity B to A? – Sumant Jun 18 '11 at 12:01
5 Answers
You need to create a class to store the variable. In ActivityB use set the value of the variable, the created class stores it and in ActivityA get the value of the variable.
Create a class: GlobalVars.java. In this class put this:
public class GlobalVars extends Application {
private static String var2; public static String getVar() { return var2; } public static void setVar(String var) { var2 = var; }
}
In ActivityB put this line in to the appropriate place:
String something;
GlobalVars.setVar(something);
In ActivityA put this line in to the appropriate place:
String getsomething = GlobalVars.getVar();
And that's it!
![](../../users/profiles/571648.webp)
- 11,491
- 31
- 126
- 226
-
Will break when your Process is killed (because it's in the background and some other app needs memory, for example), and then restarted at the second Activity. – ᆼᆺᆼ Jul 26 '11 at 14:29
-
Thanks. Kind of the easiest way when receiving activity is in onPause/onStop state. – Kunalxigxag Feb 19 '14 at 12:20
If I understand your problem correct then you want to keep dialogbox when activity B returns result. If such case then you can open dialog box onActivityResult
- Activity A
- Click on button open dialogbox
- start Activity B
- return result to Activity A
- onActivityResult will call
- open dialog box again
Note : Activity A must not be SingleTask, SingleInstance, SingleTop.
I hope it will helps
KPBird
![](../../users/profiles/756706.webp)
- 2,820
- 17
- 27
You can use the broadcast system to send an Intent containing data to another activity.
Search google or stackoverflow there are a lot of tutorials and examples of how to achieve this. as i understand you want activity a to get notified and fill a field based on some action in the dialog.
what i am suggesting is one way of doing this. the other answers provide also different solutions to the same problem. also you can register an interface with the creation of your dialog which will be called from inside the dialog and do something in the first activity.
![](../../users/profiles/448192.webp)
- 14,990
- 10
- 56
- 82
I think you need to use Bundle and static global variable and onActivityResult(). If you want to edit client with previous client to new client . Suppose you have "ClientList" Activity and "EditClient" Activity
Write into "EditClient" Activity
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String name = extras.getString(ClientList.KEY_Client);//ClientList.KEY_Client is global static variable of "ClientList" Activity.
if (name != null)
{
nameText.setText(name);//"nameText" is a EditText object represent EditText view
}
}
![](../../users/profiles/787558.webp)
- 439
- 2
- 2