activity 完成时不会调用 onActivityResult

onActivityResult is not called on when the activity is finished

我是一名 Android 新手,我需要弄清楚如何 return 从一个 activity 到另一个 activity 的数据。这个想法是,当单击回收器视图中的一行时,它将在另一个 activity 中加载选定的行。如果行被删除,则数据行被删除一个列表,控件被 returned activity 包含回收器视图。但是我收到以下错误。

Process: com.manoflogan.criminalintent, PID: 27222
    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid
view holder adapter positionViewHolder{9ed884a position=1 id=-1, oldPos=1,
pLpos:-1 scrap [attachedScrap] tmpDetached no parent}
android.support.v7.widget.RecyclerView{833ff9c VFED..... ......I.
0,0-1080,1584 #7f070032 app:id/crime_recycler_view},
adapter:com.manoflogan.criminalintent.CrimeListFragment$CrimeAdapter@4ad8da5,
layout:android.support.v7.widget.LinearLayoutManager@1abc57a,
context:com.manoflogan.criminalintent.CrimeListActivity@6a147d9
        at
android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5715)
        at
android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5898)
        at
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at
android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at
android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at
android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at
android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at
android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
        at
android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
        at
android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1877)
        at
android.support.v7.widget.RecyclerView.run(RecyclerView.java:407)
        at
android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:693)
        at
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2018-11-24 22:11:30.588 1608-1608/? E/lowmemorykiller: Error writing /proc/2

这是工作流程

  1. 当应用程序启动时,用户将看到一个空视图,要求用户 单击菜单上的 + 按钮添加 new CrimeImage # 1. This is the fragment responsible for rendering the home screen.

  2. 当单击 + 符号时,用户将被定向到 [不同的 activity,它封装了一个视图 管理片段的寻呼机。用户可以单击菜单栏上的后退按钮或设备后退按钮以在主屏幕上查看添加的条目。这是一个使用 ViewPager 来管理多个 Crime 对象的activity。

  3. 用户可以重复步骤 #2 添加更多 *crimes*.

  4. 要删除犯罪,用户 clicks/taps 在任何视图持有者(行)上转到与步骤 # 2 中相同的 activity,然后点击garbage 图标。然后该项目被删除,用户被重定向回父级 activity 屏幕管理的视图。这是调用封装所选视图的 activity 的代码块。

    private class CrimeHolder extends RecyclerView.ViewHolder implements
      View.OnClickListener {
          // Child views instance variables.
    
    
       public CrimeHolder(View view) {
           super(view);
           // *. . . Initialising child views . . . *
       }
    
       @Override
       public void onClick(View view) {
           Intent intent = CrimePagerActivity.newIntent(getActivity(), 
               mCrime.getId());
           lastAdapterClickedPosition = getAdapterPosition();
    
           // Invoking another activity. I would like the activity result to be handled by `onActivityResult`.
          getActivity().startActivityForResult(intent, 
               CRIME_VIEW_CODE);
        }
    
       void bind(Crime crime) {
          mCrime = crime;
          // Setting the state of child views
       }
    }
    

当用户点击删除按钮时,下面的菜单选项是 触发,

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    CrimeLab crimeLab = CrimeLab.get(getActivity());
    Intent intent = new Intent();
    switch(item.getItemId()) {
        case R.id.delete_crime:
            // Idea is that this index will be instantiated when activity result is returned.
            intent.putExtra(CrimeListFragment.DELETE_ROW_INDEX,
crimeLab.getCrimes().indexOf(mCrime));
            crimeLab.deleteCrime(mCrime);
            getActivity().setResult(Activity.RESULT_OK, intent);
            getActivity().finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

以下是我认为的问题。

  1. 理想情况下,当我设置了我的结果,并调用了 finish 到 return 到前一个 activity,我想要 onActivityResult在 returning 上调用。但事实并非如此。我的理解正确吗?下面给出的是我必须看到的方法。

    // Never invoked.
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == CRIME_ADD_CODE) {
           lastInsertedRowPosition = 
               data.getIntExtra(CrimeListFragment.ROW_INDEX, -1);
        } else if (requestCode == CRIME_VIEW_CODE) {
    
        }
      }
    

有两种选择先调用startActivityForResult, 替换为:

getActivity().startActivityForResult(intent, CRIME_VIEW_CODE);

startActivityForResult(intent, CRIME_VIEW_CODE);

你用 getactivity() 调用它的第二个选项,但你应该覆盖 activity 本身的 onActivityResult 以调用 ActivityResult像这样的片段:

在Activity中:

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
Fragment fragment = getSupportFragmentManager().findFragmentById("yourFragment"); 
fragment.onActivityResult(requestCode, resultCode, data); 
}

这将调用您的片段的 onActivityResult。

希望对您有所帮助。