Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call custom Activity as startActivityForResult

I am starting an Activity for result as

startActivityForResult(new Intent(this,ActivityA.class),REQUEST_CODE)

ActivityA is started. There is a gridview on ActivityA, I want to get the position of selected image in method onActivityResult(int requestCode, int resultCode, Intent data) of caller Activity but I am not getting the way to do that

like image 533
Azmat Avatar asked Jul 24 '12 09:07

Azmat


People also ask

What can I use instead of startActivityForResult?

We use startActivityForResult() to send and receive data between activities, in almost of our android projects. But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

Can startActivityForResult still be used?

Is startActivityForResult deprecated? onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.

What is request code startActivityForResult?

The request code identifies the return result when the result arrives. ( You can call startActivityForResult more than once before you get any results. When results arrive, you use the request code to distinguish one result from another.

Can we call startActivityForResult from adapter?

Yes. You can call startactivityforresult() from adapter. There are two case- 1. Calling adapter from activity and need onActivityResult in activity.


1 Answers

In Activity A,

onItemClick() of GridView

//create a new intent...
Intent intent = new Intent();
intent.putInt("position",position);
setResult(RESULT_OK,intent);
//close this Activity...
finish();

in Caller Activity,

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        super.onActivityResult(requestCode, resultCode, intent);
        Bundle extras = intent.getExtras();
        if(extras != null)
        int position = extras.getInt("position");
    }
like image 68
user370305 Avatar answered Oct 26 '22 23:10

user370305