Why is onActivityResult not called in Android?
Asked Answered
S

4

1

When I my app is launched I am showing a splash screen. That page is been shown for 10 sec, running on a thread.

When it switches over to new activity on a result I want o hit a URL in server and I will be getting a return value which I can use for my further implements.

Here is my code:

private final int SPLASH_DISPLAY_LENGHT = 1000;

new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run() 
            {
                Log.e("Handler ","run");
                Intent myIntent = new Intent(getApplicationContext(), CaptureActivity.class);
                startActivityForResult(myIntent, imgDL);
                finish();
            }
        }, SPLASH_DISPLAY_LENGHT);



public void onActivityResult(int requestCode, int resultCode, final Intent data) 
      {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == imgDL) 
          {     
              Log.e("onActivity Result","");
              urlHitMethod("http://XXXXXXXXXXXXXXXXXX.com/banner_scan");
          }
      }

But here the onActivityResult is not called. How to fix this?

Secondhand answered 19/7, 2011 at 4:53 Comment(5)
Why are you finishing your caller activity?Diarthrosis
in my next activity when the user clicks the back button i want the app to get closed....Secondhand
when u using finish then it will never come in onActivityResult so remove itMoran
Can you post more codes of caller Activity and some lines of called Activity where you finishing that?Diarthrosis
now i am trying this for a sample purpose, the above code is my caller activity and in my called activity i am just showing a layout without any codes. just typed as setContentView(R.layout.image);Secondhand
E
0

In the CaptureActivity.class you have to set the result and then check in the onActivityResult in the First Activity the result code

In the CaptureActivity.class it should be like the following

 Intent in = new Intent();
    setResult(1,in);//Here I am Setting the Requestcode 1, you can put according to your requirement
    finish();
Exclamation answered 19/7, 2011 at 5:5 Comment(3)
Actually your comment is wrong. You don't set the requestCode, but the resultCode. And if I'm right, you could use Activity.RESULT_CANCELLED and Activity.RESULT_OKTupi
My problem is that when you set the result of the activity to RESULT_OK, onActivityResult on the waiting activity was never called.When i changed to result value to 1 it worked.Wadley
-1. @Tupi is right - you aren't setting the requestCode, you're passing a literal value in for resultCode, which actually corresponds to RESULT_FIRST_USER, not RESULT_OKUngenerous
M
4

Also, please note than if you base activity (the one calling startActivityForResult) can't use the flag noHitory in the manifest.

If you do so, onActivityResult will never be called.

Marianamariand answered 27/6, 2012 at 9:15 Comment(0)
M
1

try this

Intent myIntent = new Intent(activity.this, CaptureActivity.class);

and

@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) 
      {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == imgDL) 
          {     
              Log.e("onActivity Result","");
              urlHitMethod("http://XXXXXXXXXXXXXXXXXX.com/banner_scan");
          }
          if(resultCode==RESULT_OK)
      {
    Log.e("onActivity Result","come in onactivity result ok"); 

      }
          else
          {
    Log.e("onActivity Result","come in onactivity result with error"); 

      }



      }
Moran answered 19/7, 2011 at 5:0 Comment(1)
when u using finish then it will never come in onActivityResult so remove itMoran
M
1

If you are using onActivityResult, then you should not finish the activity when starting with intent otherwise it will crash the app. Thanks.

Miquelon answered 10/10, 2011 at 7:2 Comment(0)
E
0

In the CaptureActivity.class you have to set the result and then check in the onActivityResult in the First Activity the result code

In the CaptureActivity.class it should be like the following

 Intent in = new Intent();
    setResult(1,in);//Here I am Setting the Requestcode 1, you can put according to your requirement
    finish();
Exclamation answered 19/7, 2011 at 5:5 Comment(3)
Actually your comment is wrong. You don't set the requestCode, but the resultCode. And if I'm right, you could use Activity.RESULT_CANCELLED and Activity.RESULT_OKTupi
My problem is that when you set the result of the activity to RESULT_OK, onActivityResult on the waiting activity was never called.When i changed to result value to 1 it worked.Wadley
-1. @Tupi is right - you aren't setting the requestCode, you're passing a literal value in for resultCode, which actually corresponds to RESULT_FIRST_USER, not RESULT_OKUngenerous

© 2022 - 2024 — McMap. All rights reserved.