Noman
Noman

Reputation: 4109

OnActivityResult not working (Android)

I am using a simple function OnActivityResult, but it is not returning me desired results.

Please see my code and tell me where i am doing wrong.

public void OnClickFunction(View view)
{
    Intent intent = new Intent(getApplicationContext(), Second.class);
    startActivityForResult(intent, RESULT_OK);

///     My actions. . . 
}    

Then in the Second class, i have set Result like this:

Button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            ValueOne = EditText.getText().toString().trim();
            if (ValueOne.equals(String.valueOf(Answer)))
            {
                Toast.makeText(getApplicationContext(), "Correct Answer", 0).show();
                Second.this.setResult(RESULT_OK, null);
                Second.this.finish();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Wrong Answer", 0).show();
            }
        }
    });    

Now coming back to the first.class, from where the Intent was called:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
//      if (requestCode == RESULT_OK)
//      {
        if (resultCode == RESULT_OK)
        {
                             /////  MyActions. . . 
        }
//      }
}    

The debugger is not debugging this function, so the desired results are not coming.
Where i am doing wrong??

Upvotes: 9

Views: 17485

Answers (3)

S.Hossein Emadi
S.Hossein Emadi

Reputation: 89

in my case: when use activityForResult, I add this flag :

addFlags(FLAG_ACTIVITY_NEW_TASK)

when delete this flag ,fixed

Upvotes: -1

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

You have to destroy the second activity. Try pressing back button. I am able to see all the log messages in onActivityResult

First Activity

public class FirstActivity extends Activity {
/** Called when the activity is first created. */
int result = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent i = new Intent(this,SecondActivity.class);
    startActivityForResult(i, result);
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    Log.i("H", "RequestCode:" + requestCode);
    Log.i("H", "ResultCode:" + resultCode );
}
}

SecondActivity

public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setResult(RESULT_OK);
    Log.i("S","Exiting Second Activity");
}
}

Upvotes: 4

Abhi
Abhi

Reputation: 9005

in Source Class:

int activity=1;
Intent i=new Intent(Sourceclass.this,destination.class);
startActivityForResult(i,activity);

In Destination class:

Intent i=new Intent();
      setResult(RESULT_OK,i);
    finish();

In OnActivityResult of Source Class:

public void onActivityResult(int requestCode, int resultCode, Intent data) 
 {
    if (resultCode == RESULT_OK) 
   {
                   if(requestCode==1)
            {
                Log.e("check","check");

            }
   }

}

Upvotes: 2

Related Questions