How to use onActivityResult method from other than Activity class

前端 未结 7 1208
鱼传尺愫
鱼传尺愫 2020-12-05 23:53

I am creating an app where i need to find current location of user .

So here I would like to do a task like when user returns from that System intent, my task shou

7条回答
  •  臣服心动
    2020-12-06 00:47

    When you start an activity with startActivityForResult method from an activity, only the caller will receive the result.

    So you could handle the result and pass it to the task or update the UI of that activity:

    int MY_REQUEST_ID = 1;
    
    public void onClick(){
        //Select a contact.
        startActivityForResult(
                 new Intent(Intent.ACTION_PICK,
                 new Uri("content://contacts")),
                 MY_REQUEST_ID);
    }    
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if(requestCose == MY_REQUEST_ID && resultCode == SUCCESS) {
             MyAsyncTask task = new AsyncTask(requestCode, resultCode, data);
             task.execute();
             // or update the UI
             textView.setText("Hi, activity result: "+ resultCode);
         }
    }
    

提交回复
热议问题