2

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).

Ovidiu Birgu
  • 429
  • 2
  • 6
  • 24

5 Answers5

4

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.

  1. 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!

erdomester
  • 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
1

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

  1. Activity A
  2. Click on button open dialogbox
  3. start Activity B
  4. return result to Activity A
  5. onActivityResult will call
  6. open dialog box again

Note : Activity A must not be SingleTask, SingleInstance, SingleTop.

I hope it will helps

KPBird

Ketan Parmar
  • 2,820
  • 17
  • 27
0

Perhaps try using sharedpreferences !?

sisko
  • 8,609
  • 18
  • 58
  • 121
0

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.

DArkO
  • 14,990
  • 10
  • 56
  • 82
0

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
      }

  }
rabby
  • 439
  • 2
  • 2