活动结束后,onActivityResult未打开

时间:2018-11-25 09:14:39

标签: android android-fragments android-intent android-fragmentactivity

我是一名Android新手,我需要弄清楚如何将数据从一个活动返回到另一个活动。这个想法是,当单击“回收者”视图中的一行时,它将在另一活动中加载选定的行。如果该行被删除,则该数据行将被删除一个列表,并且该控件将返回包含回收者视图的活动。但是我遇到了以下错误。

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$1.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 Crime按钮来添加+Image # 1。这是fragment responsible for rendering the home screen

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

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

  4. 要删除犯罪,用户单击/轻击任何视图持有人(行)以进行与步骤#2中相同的活动,然后点击garbage图标。然后删除该项目,并将用户重定向回由父级活动屏幕管理的视图。这是调用封装所选视图的活动的代码块。

    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
       }
    }
    

当用户单击删除按钮时,following menu option is triggered

@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返回上一个活动时,我希望在返回时调用onActivityResult。但事实并非如此。我的理解正确吗?下面给出的是我必须看到的调用方法。

    // 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) {
    
        }
      }
    

1 个答案:

答案 0 :(得分:0)

有两个选项,首先调用startActivityForResult, 替换为:

getActivity().startActivityForResult(intent, CRIME_VIEW_CODE);

使用

startActivityForResult(intent, CRIME_VIEW_CODE);

使用getactivity()调用它的第二个选项,但是您应该从活动本身覆盖onActivityResult,以像下面这样调用片段的onActivityResult:

活动中:

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

,这将调用片段的onActivityResult。

希望这对您有所帮助。