0

1st Activity

 Intent in=new Intent(Activity1.this, Activity2.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("username",welcomeusername );
                    in.putExtras(bundle);
                    startActivityForResult(in, 2);

In 2nd Activty

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("Log.d in activity", "Executing perfectly");
        // TODO Auto-generated method stub
        switch(requestCode)
        {

          case 2: 
            if  (resultCode == RESULT_OK) 
            { 
                    Log.d("into onActivity result", "Successss");

                    Bundle bn=data.getExtras();
                    username=bn.getString("username");
                    Log.d("usssername", username);
                    welcometextview.setText(username);
            }
            break;
          }

But the onActivityResult method isnt called. I have declared both activities in manifest.xml, but the control isn't transferred to the onActivityresult after execution of oncreate in Activity2.

halfer
  • 19,824
  • 17
  • 99
  • 186
AndroidMech
  • 1,676
  • 3
  • 20
  • 31
  • dupliate :http://stackoverflow.com/questions/12233106/really-not-getting-setresult-and-onactivityresult – blay Nov 20 '13 at 09:06
  • you should override the `onActivityResult()` in the `Activity1` which is waiting for result from the `Activity2` – Houcine Nov 20 '13 at 09:15
  • thanks a lot guys for the help..looks like i messed up my concept of onActivityResult.. – AndroidMech Nov 20 '13 at 09:36

4 Answers4

2

in 1st Activity

 Intent in=new Intent(Activity1.this, Activity2.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("username",welcomeusername );
                    in.putExtras(bundle);
                    startActivityForResult(in, 2);


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("Log.d in activity", "Executing perfectly");
        // TODO Auto-generated method stub
        switch(requestCode)
        {

          case 2: 
            if  (resultCode == RESULT_OK) 
            { 
                    Log.d("into onActivity result", "Successss");

                    Bundle bn=data.getExtras();
                    username=bn.getString("username");
                    Log.d("usssername", username);
                    welcometextview.setText(username);
            }
            break;
          }

In second one add Intent intent = getInntent(); intent.put.(whatever you want to put, sting parceable etc etc)

setresult(RESULT_OK,intent ); finish();

now in first activity onactivityresult will be called. study about request code and response code as well. cheers :)

Rajiv yadav
  • 823
  • 8
  • 24
1

I think you made something wrong.

Activity 1 start a new activity (with startActivityForResult) So Activity 1 is 'waiting' for a result

Your override of onActivityResult() should be in you Activity 1 and not in your second activity.

azerto00
  • 1,001
  • 6
  • 18
1

you just have to write getIntent.getStringExtra("username") on second activity to get the string passing from first activity.
onActivityResult() is call when you come back from 2nd activity. so you have to override the method to 1st activity.
It is useful when you want to pass and handle some values from Activity(here 2nd activity) to its calling activity (here 1st Activity).

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
1

Move the code below to the onCreate() on the 2nd Activity.

Bundle bn=data.getExtras();
username=bn.getString("username");
Log.d("usssername", username);
welcometextview.setText(username);

The onActivityResult() is called only when an activity start an another activty and the execution is returned from the 2nd activty to the 1st activity. In your case you are passing the data to the second activity in the Intent itself. So all you got to do is read the data on OnCreate().

SathMK
  • 1,171
  • 1
  • 8
  • 18