3

This is a basic question but I need some help with it.

I have two activities : actA, actB. While in actA I want to start actB and give it a String, than I want to end actB and return another String to actA (I don't want to go to onCreate() of actA, I would much rather return this value to some method in actA so it can use the String from actB.

Help is appreciated

Belgi
  • 14,542
  • 22
  • 58
  • 68

4 Answers4

3
From A.java:
Intent myintentB=new Intent(A.this, B.class).putExtra("<StringName>", "Value");
    startActivityForResult(myintentB, 3);

    from B.java:

    Intent myintentA=new Intent(B.this, A.class).putExtra("<StringName>", "Value"); 
    finish();
    setResult(3, myintentA);


    In A.java
@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            String result_string=data.getStringExtra("<StringName>");
        }
Pinki
  • 21,723
  • 16
  • 55
  • 88
2

In Activity A :

Intent intent = new Intent();
intent.setClass (getApplicationContext(), ActB.class) ;
intent.putExtra ("data1","NEW STRING") ;
context.startActivityForResult(intent) ;

In Activity B (onCreate Method) :

Intent intent = getIntent() ;
if (intent.hasExtra("data1") )
String dataSent = intent.getStringExtra("data1") ;

While sending data back :

Intent intent = new Intent() ;
intent.putExtra ("Return" , "RETURN STRING") ;
setResult(RESULT_OK, intent) ;
finish() ;

In Activity A : (onActivityResult) [ You need to override ]

if (data.hasExtra("Return"))
String data1 = data.getStringExtra("Return");
Vinay
  • 2,395
  • 3
  • 30
  • 35
  • I tried using the first part of the code in actA and I get two errors: 1. The method setClass(Context, Class>) in the type Intent is not applicable for the arguments (Class) 2.context cannot be resolved – Belgi Oct 19 '11 at 12:25
  • For context not resolved, say getApplicationContext(). For setClass include context as needed. – Vinay Oct 19 '11 at 12:46
0

the answer to question linked below also describes the same issue you are looking for

How to pull string from bundle in onResume()?

Community
  • 1
  • 1
Umar Qureshi
  • 5,985
  • 2
  • 30
  • 40
-1

As for your first problem, you can give extra values to a new Intent by using the method intentname.putExtra("extravalue", value); before actually creating the Intent. You can then read that value out in the the newly created Activity. I know I'm pretty terrible at explaining, but I hope you get the idea from this piece of code I took from an app I made.

ListLinks.java:

            // Pass the value of the item URL to the linkviewer when a link is clicked
        Intent openLink = new Intent(this, LinkView.class);
        openLink.putExtra("url" , item.URL);
        startActivity(openLink);

LinkView.java

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.linkview);

    Bundle bun = getIntent().getExtras();
    String url = bun.getString("url");

I'm not quite sure about how to resolve your second problem, so sadly I cannot help you with this one but I'm sure someone else might.

Sander van't Veer
  • 5,930
  • 5
  • 35
  • 50