android - startActivityForResult 似乎没有调用 onActivityResult

标签 android

当用户单击要调用对话框的按钮时 - 该对话框包含 ListView 中的产品列表。用户选择产品后,它应该进入上一个 Activity 。

我已经使用 startActivityForResult () 完成了。

有一些问题。我的调用 Activity 在正常标签 Activity 中,正常标签 Activity 在标签 Activity 组中。

实际上我想在 drrop down(Spinner) 中做。在我的 scanerio 中我无法获取上下文。它 awalys 给 Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window

所以我对我的设计进行了这样的更改:当用户单击按钮时,它会在 ListView 中加载产品列表。选择产品后,它会返回到之前的 Activity 。

This is my previous question : link

这里调用 Activity :

  //Click Product button
   l_prod.setOnClickListener(new OnClickListener() {
       public void onClick(View v) {
            Intent showContent = new Intent(LineDiscountActivity.this,ListProductActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("Activity", "LineDiscountActivity");
            bundle.putString("RetailerName", retailerName);
            bundle.putString("RetailerCode", retailerCode);
            showContent.putExtra("discountProduct", discountList);
            showContent.putExtras(bundle);
            getParent().startActivityForResult(showContent, 5);
       }
   });

还有我的接收器 Activity :

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
            Object o = this.getListAdapter().getItem(position);
            String book = o.toString();
            Intent i = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("Activity", "ListProductActivity");
            bundle.putString("RetailerName", retailerName);
            bundle.putString("RetailerCode", retailerCode);
            bundle.putString("seletcedProductCode", products.get(position).getProductCode());
            bundle.putString("seletcedProductName", products.get(position).getDescription());
            bundle.putDouble("seletcedProductQty", products.get(position).getAvailableQuantity());
            i.putExtra("discountProduct", discountList);
            i.putExtras(bundle);
            if (getParent() == null) {
                setResult(Activity.RESULT_OK, i);
            } else {
                getParent().setResult(Activity.RESULT_OK, i);
            }
            ListProductActivity.this.finish(); 

    }

和通话 Activity

 @Override
protected  void onActivityResult(int requestCode, int resultCode, Intent data) {
//  super.onActivityResult(requestCode, resultCode, data);
    Log.i("-requestCode from LineDisocunt--" ,"" + requestCode);

}

我已经在调用 Activity 和 Tab 主 Activty 中编写了这段代码 (onActivityResult)。

我哪里都没去..

onActivityResult mehtod.But it didn't go it.

我的代码有什么问题。

如果有人知道请告诉我...

提前致谢

最佳答案

当我将 startActivityForResult()activity group 一起使用时,我遇到了同样的问题。

您的 Activity 结果将进入您的 Activity 组。您不会在您的第一个 Activity 中获得 Activity 结果

所以你可以通过在你的第一个 Activity 中使用一个公共(public)静态对象来解决这个问题,当你调用第二个 Activity 时,你必须从第二个 Activity 中分配你的第一个 Activity 对象。然后完成第二个 Activity ,这样你的第一个 Activity 就会恢复并且您可以通过覆盖第一个 Activity 中的 onResume() 方法来更新您的用户界面。您必须检查是否分配了对象的验证天气。

例如

您的第一个 Activity 中有一个静态对象产品

第一个 Activity

public static Product product;
start second activity
startactivity(this, SecondActivity.class);

don't finish First Activity

您必须覆盖onResume() 方法然后您可以使用由第二个 Activity 分配的产品对象

第二个 Activity

FirstActivity.product.setName(name);
FirstActivity.product.setPrice(price);

分配产品对象后,您必须完成第二个 Activity 喜欢

finish()

编辑

我得到了你的 badTokenException 问题的解决方案

解决方法

CLICK HERE

关于android - startActivityForResult 似乎没有调用 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7645489/

相关文章:

android - 如何将 FAB 转换为弹出菜单?

java - 什么安卓:inputType should I use for entering an IP Address and hostname?

android - 无法启动模拟器 : "Emulator: Warning: requested RAM 1024M too high for your system. Reducing to maximum supported size 0M"

android - 如何在 Android (Nexus 10) 上禁用 Chrome 30 中的双击缩放功能

用于 http 地址的 Android Intent-Filter

Android 应用内安全建议 - 这是什么意思?

java - DDMS java 分析器显示 : java/lang/StringBuilder. <init> - 它在我的代码中的什么位置?

java - 为什么我的方法返回空值?

java - Android 中的 ListActivities 如何与 ListView 配合使用?

java - 如何优化图像处理类