startActivityForResult

startActivityForResult

本文介绍了从自定义调用的ViewGroup startActivityForResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从LinearLayout中扩展自定义组的看法:

I have a custom group view that extends from LinearLayout:

class MyCustomView extends LinearLayout {
    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        onCreateView(context);
    }

    public MyCustomView(Context context, int checkableId) {
        super(context);
        onCreateView(context);
    }

    private void onCreateView(Context context){
        View.inflate(context, R.id.my_custom_view, null);
    }
}

在该自定义组视图我有一个按钮,当用户点击该按钮我需要调用与startActivityForResult的活动,因为我需要从活动的回报。

In that custom group view I have a button and when the user clicks that button I need to call an activity with startActivityForResult because I need the return from that activity.

不过的LinearLayout没有实现startActivityForResult既不的onActivityResult。

But the LinearLayout doesn't implement the startActivityForResult neither the onActivityResult.

所以我的问题是:我怎么能调用startActivityForResult从MyCustomView

So my question is: how can I call startActivityForResult from "MyCustomView"?

推荐答案

据我所知,你有两个选择:

As far as I can tell, you have two options:


  1. 使用 MyCustomView 在扩展活动片段或 FragmentActivity 并调用 startActivityForResult()从那里。

  2. 通过你的活动的实例 MyCustomView (可能通过构造函数),并使用 instanceOfYou​​rActivity.startActivityForResult() 。这hovewere,可能是不安全的,造成的副作用(例如,你可能无法赶上的onActivityResult())。

  1. Use MyCustomView in a class that extends one of flavors of Activity, Fragment or FragmentActivity and call startActivityForResult() from there.
  2. Pass the instance of your activity to MyCustomView (possibly through the constructor) and use instanceOfYourActivity.startActivityForResult(). This, hovewere, could be unsafe and cause side effects (e.g., you may not be able to catch onActivityResult()).

这篇关于从自定义调用的ViewGroup startActivityForResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 04:53