Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 646
Noman Avatar asked Dec 21 '11 09:12

Noman


People also ask

How does onActivityResult work on Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

What is alternative for startActivityForResult in Android?

In this article, we will discuss the Activity Result API, an alternative to the deprecated startActivityForResult + onActivityResult methods. We will use Kotlin in this article for the code samples. Android introduced the Activity Result APIs as a major change in the androidx.

Can I use onActivityResult in fragment?

Since Activity gets the result of onActivityResult() , you will need to override the activity's onActivityResult() and call super. onActivityResult() to propagate to the respective fragment for unhandled results codes or for all.


2 Answers

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");
}
}
like image 96
Rajdeep Dua Avatar answered Oct 04 '22 10:10

Rajdeep Dua


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");

            }
   }

}
like image 42
Abhi Avatar answered Oct 04 '22 09:10

Abhi