0

I'm new to android . This might be the simplest question of all !! but I couldn't figure out whats gone wrong here,

I was trying to create a basic example for passing values through intent.So I need to pass data to Main Activity when I close my Second Activity here is the code

IntentTest1(MainActivity)

public void onClick(View arg0) {
            // TODO Auto-generated method stub
MyClass.myToast("Clicked",getApplicationContext());
Intent myIntent = newIntent(getApplicationContext(),SecondPage.class);
startActivityForResult(myIntent,0); 
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    if(requestCode == 0 && resultCode == RESULT_OK)
        if(data.hasExtra("title"))
        {
    MyClass.myToast(""+resultCode+""+requestCode, getApplicationContext());
    String str = data.getExtras().getString("title").toString();
    titleText.setText(str); 
        }
    super.onActivityResult(requestCode, resultCode, data);
}

SeconPage

public void finish()
{

Intent returnIntent = new Intent(getApplicationContext(),Intenttest1.class);
returnIntent.putExtra("Welcome Back!!","title");
setResult(RESULT_OK, returnIntent);
    //      below was for tosting and it works!!
MyClass.myToast("finally",getApplicationContext());
super.finish();
}

I think there is some mistake in receiving the intent ,I couldn't figure out. Answers and Advises are needed thanks in advance

Dev
  • 31
  • 5

3 Answers3

2

The first problem is when you create your Intent to send back to the first Activity. Since you are using startActivityForResult() you want to use an empty constructor. So change

Intent returnIntent = new Intent(getApplicationContext(),Intenttest1.class);

to

Intent returnIntent = new Intent();

The second problem is that you have your key/value pair backwards in your Extras. The key, which is what you look for with getStringExtra() etc... should be the first in the pair. So this

returnIntent.putExtra("Welcome Back!!","title");

should be

returnIntent.putExtra("title", "Welcome Back!!");

Off-topic

I would consider using relevant names as your params. For example, I would change your onClick() from

public void onClick(View arg0)

to

public void onClick(View view)  

view, v, or something similar makes more sense since the argument actually is a view and it will be more readable

I would also recommend using the Activity Context for your Intent which you can get from the argument (the View) passed into onClick(). So change it to

public void onClick(View v) 
{
     MyClass.myToast("Clicked",getApplicationContext());
     Intent myIntent = newIntent(v.getContext(),SecondPage.class);
     startActivityForResult(myIntent,0); 
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • You're welcome! And to answer your question on the other answer about `Context` and where to use `this`, [see this very good answer](http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context/7298955#7298955) – codeMagic Dec 23 '13 at 14:22
  • If you use `this` inside a `listener`, such as `onClick()`, it will typically be wrong because it will refer to the inner class and not the `Activity`. – codeMagic Dec 23 '13 at 14:22
  • One little Doubt is why we are passing 0 with startActivityForResult? I didn't use that value anywhere else in my example code. – Dev Dec 23 '13 at 14:28
  • Look at the documentation but that gets passed back to `onActivityForResult()` as the `RequestCode`. You could have different static variables in case you have multiple `startActivityForResult()`s in the same `Activity` such as calling the camera, picking contacts, etc... – codeMagic Dec 23 '13 at 14:31
1

You have to use

if(data.hasExtra("Welcome Back!!"))

instead of

if(data.hasExtra("title")) 

in onActivityResult. Welcome Back!! is the key and title is the value for that key in your extras.

Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • Got it Thanks a lot , I should have been more careful when I am writing code :) – Dev Dec 23 '13 at 14:18
0

try this code:

Intent returnIntent = new Intent(getApplicationContext(),Intenttest1.class);
returnIntent.putExtra("Key name here in ur case title","Value name");
setResult(RESULT_OK, returnIntent);
//      below was for tosting and it works!!
 MyClass.myToast("finally",getApplicationContext());
super.finish();

}

rupesh
  • 2,865
  • 4
  • 24
  • 50
  • nope!!:( its not working!! Thanks Anyway and Now I've one more doubt? If it'll work? whats the difference? where I can use 'this' and where I can't .. – Dev Dec 23 '13 at 14:15