Why is onActivityResult not called in Android?

独自空忆成欢 提交于 2019-12-11 07:55:45

问题


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?


回答1:


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



回答2:


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.




回答3:


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

      }



      }



回答4:


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



来源:https://stackoverflow.com/questions/6742438/why-is-onactivityresult-not-called-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!