从另一个 class 调用 startActivityForResult?

Calling startActivityForResult from a different class?

我的 Activity 中有一个从左侧滑动的抽屉。我通常通过单击按钮调用 startActivityForResult,这通常工作正常。该按钮在我的 Activity 中实例化,因此 startActivityForResult 和 Activity 结果工作正常。

由于我已经实现了抽屉并从 selectItem 方法调用 startActivityForResult(启动相机),所以相机没有启动。 我认为这是因为 startActivityForResultonActivityResult 需要在同一个 class 中才能使机制起作用。

如何从抽屉中调用 startActivityForResult 并将结果发送到外部 Activity。

提前致谢。

     public void onActivityResult(int requestCode, int resultCode, Intent intent) {
                Log.e(TAG, "in onActivityResult");
                if (requestCode == 0) {
                    if (resultCode == RESULT_OK) {
                        Log.e(TAG, "result ok");

        }
        ......

        private class DrawerItemClickListener implements ListView.OnItemClickListener {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {

                    String rowTitle = view.getTag().toString();

                    selectItem(position, rowTitle);

                }


            }

            /** Swaps fragments in the main content view */
            private void selectItem(int position, String rowTitle) {




                // Highlight the selected item, update the title, and close the drawer
                mDrawerList.setItemChecked(position, true);
                //setTitle(mPlanetTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);

                //"SIGN IN/OUT", "SEND OUTSTANDING TRANSACTIONS", "SIGNOUT MANUALLY", "LOGS", "ASSESSMENTS"

                if(rowTitle.equalsIgnoreCase("SIGN IN/OUT")){

                                    Log.e(TAG, "onclicked sign in");

        Intent intent = new Intent("com.carefreegroup.rr3.SCAN");
    intent.putExtra("SCAN_MODE","QR_CODE_MODE");            
startActivityForResult(intent, 0);

        }

。 编辑 1

adapter2 = new MySimpleArrayAdapter(this, lst2);

mDrawerList.setAdapter(adapter2);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

使用startActivityForResult() to start an activity should be used to do some work on that new activity, and return the result to the caller by calling the setResult(int resultCode) or setResult(int resultCode, Intent data)方法。

For example, your app can start a camera app and receive the captured photo as a result. Or, you might start the People app in order for the user to select a contact and you'll receive the contact details as a result.

因此,除非您不在新 activity 上调用 setResult()finish(),否则您的调用者 activity 将永远不会收到 [=12= 上的结果]回调。

您绝对应该阅读 this 以获得详尽的解释。 也可以考虑阅读 this article

希望对您有所帮助。