我们可以从适配器调用startActivityForResult吗?如何获得响应?

时间:2013-11-16 07:25:18

标签: android

是否可以在适配器中使用方法startActivtyForResult?然后如何获得响应?在哪里执行回叫功能?

5 个答案:

答案 0 :(得分:44)

是的,这是可能的。您需要适配器中Context的引用并调用活动:

Intent intent = new Intent(context, TargetActivity.class);
((Activity) context).startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);

请注意,上下文必须是活动上下文,否则此代码将失败。

您可以像往常一样使用onActivityResult在封闭活动中获得结果。

所以,例如:

在适配器中:

MyAdapter(Context context) {
    mContext = context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    …
    open.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            …
            Activity origin = (Activity)mContext;
            origin.startActivityForResult(new Intent(mContext, SecondActivity.class), requestCode);
        }   
    });
    …
}

public  void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("MyAdapter", "onActivityResult");
}

在第二项活动中,照常使用setResultfinish

在主活动中,捕获结果并传递给适配器回调:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mAdapter.onActivityResult(requestCode, resultCode, data);
}

答案 1 :(得分:7)

是的。您可以从适配器调用startactivityforresult()。

有两种情况 - 1.从活动中调用适配器,并在活动中需要onActivityResult。 2.从Fragment调用适配器,并在片段中需要onActivityResult。

案例1:在Activity中获取OnActivityResult,然后将activity的引用传递给适配器构造函数

public MyAdapter(Activity pActivity, List<MyBean> pList) {
        mList = pList;
        mActivity = pActivity;       
    }

现在startActivityForResult

Intent intent = new Intent(context, TargetActivity.class);
mActivity.startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);

案例2:在Fragment中获取OnActivityResult然后将片段的引用传递给适配器构造函数

 public MyGamesAdapter(Fragment pContext, List<MyBean> pList,) {
        mList = pList;
        mMyFragment =pContext;
    }

 Intent intent = new Intent(context, TargetActivity.class);
    mMyFragment.startActivityForResult(intent, REQUEST_FOR_ACTIVITY_CODE);

现在在活动或片段中覆盖OnActivityResult并获得结果。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mAdapter.onActivityResult(requestCode, resultCode, data);
}

答案 2 :(得分:1)

我使用了一种更简单的方法。在您的活动/片段中创建一个公共功能,该活动将为结果调用活动

public void startActivityFromAdapter(String Arguments){
    //todo: add steps you would like to compute
    startActivityForResult(Intent, REQ_CODE);
}

创建适配器时,将当前活动/片段作为参数传递。我将以活动为例

public MyAdaper(Activity activity, ArrayList<String> list){
    this.activity = activity;
    this.list = list;
}

通过将活动强制转换为“活动类”,从视图所有者中调用公共功能

 @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    
    holder.applyBt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((ActivityName) activity).startActivityFromAdapter(list.get(position).code);
        }
    });
}

并在通话活动/片段中使用onActivityResult

答案 3 :(得分:0)

在活动类中编写一个函数,如下所示

public void startCommentActivity(Intent i){
    startActivityForResult(i, 100);
}

在适配器类中调用它

mActivity.startCommentActivity(intent);

答案 4 :(得分:0)

androidX Activity实现has a new set of APIs可以在您希望的任何位置获得活动结果。

此功能替代了ComponentActivity中已弃用的旧版 startActivityForResult()/ onActivityResult() onRequestPermissionsResult()

示例开始第二个活动并从中获得结果:

final Intent intent = new Intent(context, SecondActivity.class);
final ActivityResultContracts.StartActivityForResult contract = new ActivityResultContracts.StartActivityForResult();

activity.registerForActivityResult(contract, result ->
    YourClass.manageTheResult(result))
    .launch(intent);

请求用户权限的示例:

final String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};

final ActivityResultContracts.RequestMultiplePermissions contract = new ActivityResultContracts.RequestMultiplePermissions();

activity.registerForActivityResult(contract, result -> {
    YourClass.manageTheResult(permissions, result);
}).launch(permissions);

管理“发件人意图”的示例:

final IntentSenderRequest request = new IntentSenderRequest.Builder(yourIntentSender)
    .build();

final ActivityResultContracts.StartIntentSenderForResult contract = new ActivityResultContracts.StartIntentSenderForResult();

activity.registerForActivityResult(contract, result ->
    YourClasss.manageTheResult(result))
    .launch(request);