开发者

How to pass string between two activities

开发者 https://www.devze.com 2023-03-14 00:52 出处:网络
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 us

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 quest开发者_如何学编程ion 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).


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!


It sound like 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.


Perhaps try using sharedpreferences !?


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.


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
      }

  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号