在activitygroup中用startActivityForResult

I've had a similar issue. I had an ActivityGroup managing sub-activities. One of the sub-activities called a similar external intent (external to my app). It never called the onActivityResult within the sub-activity that started it.

I finally figured out/remembered that the issue is because Android will only allow a nested layer of sub-activities...ie sub-activities can't nest sub-activitites. To solve this:

  1. call getParent().startActivityForResult() from your sub-activity
  2. your parent (the activitygroup) will be able to handle theonActivityResult. So I created a subclass of ActivityGroup and handled thisonActivityResult.
  3. You can re-route that result back to the sub-activity if you need to. Just get the current activity bygetLocalActivityManager().getCurrentActivity() . My sub-activities inherit from a custom activity so I added ahandleActivityResult(requestCode, resultCode, data) in that subclass for theActivityGroup to call.

 

 

 

 

 

 

 

在Activitygroup中设置onKeyDown按键响应事件:

在activitygroup中直接监听onkeydown事件是不行的,可以尝试用

@Override
 public boolean dispatchKeyEvent(KeyEvent event) {
  // TODO Auto-generated method stub

  if(event.getKeyCode()==KeyEvent.KEYCODE_BACK)
  {
   Builder builder=new AlertDialog.Builder(this);
   builder.setTitle("提示").setMessage("确定要退出吗?")
   .setPositiveButton("确定", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int arg1) {
     // TODO Auto-generated method stub
     MainTabActivity.this.finish();

    }
   })
   .setNegativeButton("取消",null).create().show();
   //return false;
  }
  //else
   return super.dispatchKeyEvent(event);
 }

 

但如上代码会触发两次对话框的弹出,这是因为当松开按键,此时会再次触发该事件,但不同的是该evetnt中的action为up上次为down所以根据这个判断只要修改为

event.getKeyCode()==KeyEvent.KEYCODE_BACK&&
event.getAction()==KeyEvent.ACTION_DOWN

就可以了

 

 

 

 

 

 

昨天发现一个问题,是当我用ActivityGroup实现多activity跳转的时候用户按下back键直接从子activity退出应用,没有响应到activitygroup的onkeydown事件.其实可以在每个子activity覆写onkeydown事件弹出退出应用提示,但是就是想搞清楚为什么在子activity上按back就直接退出了而不响应到主activitygroup的onkeydown事件.如果可以响应,那么我们在主activityGroup里写一个退出提示框就可以了.这样不是可以省点代码吗.后来发现子activity是listview的时候里边的数据发生变化后就会先响应自己的onkeydown事件,如果子activity是非listview的可以直接响应到主activityGroup的onkeydown事件.(这里的前提是每个子activity都不覆写onkeydown事件只有activityGroup实现这个事件)后来查资料实验后给listview覆写onkeydown事件但是返回false.这样就可以将事件传递到主activityGroup里去响应了.总之比在子activity里覆写onkeydonw事件实现一堆逻辑要少一些代码的.子listview可以写:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK) {
   return false;
  } else {
   return super.onKeyDown(keyCode, event);
  }
}

主acitivtyGroup可以写:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
  if (keyCode == KeyEvent.KEYCODE_BACK) {
   quit();//自己实现的弹出提示框,这个大家都会就不详细写了.
   return true;
  } else {
   return super.onKeyDown(keyCode, event);
  }
}

以下是我查到的资料,很有用.

键盘事件只会发送到当前获得焦点的View,这个KeyEvent只能被最上层获得焦点窗口的activity和view得到。一般来说这些事件会从上倒下去寻找合适的接受组件,ViewGroup的一个childView的onKeyDown()方法return true,那么表示该方法消费了此次事件,此时不会再传递到ViewGroup的onKeyDown()方法,如果onKeyDown()方法return false,那么表示该方法并未处理完全,该事件仍然需要以某种方式传递下去继续等待处理,这时传递到ActivityGroup的onKeyDown()方法。但是KeyEvent.KEYCODE_MENU不会传递到ActivityGroup去。

一个ActivityGroup的键盘事件处理
按下按键时执行
首先执行ActivityGroup dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_DOWN)
然后执行ActivityGroup onUserInteraction
最后执行ActivityGroup onKeyDown

松开按键时执行
首先执行ActivityGroup dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_UP)
然后执行ActivityGroup onUserInteraction
最后执行ActivityGroup onKeyDown

如果childView是listView,按上下键选择列表时的事件执行流程
按下键时
首先执行ActivityGroup dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_DOWN)
然后执行ActivityGroup onUserInteraction
然后执行childActivity dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_DOWN)
然后执行childActivity onUserInteraction 
松开键时
首先执行ActivityGroup dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_UP)
然后执行ActivityGroup onUserInteraction
然后执行childActivity dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_UP)
然后执行childActivity onUserInteraction
然后执行childActivity onKeyUp
最后执行ActivityGroup onKeyUp


如果焦点在childView的listView上,按Menu键的事件
首先执行ActivityGroup dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_DOWN)
然后执行ActivityGroup onUserInteraction
然后执行childActivity dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_DOWN)
然后执行childActivity onUserInteraction 
最后执行childActivity onKeyDown
松开键时
首先执行ActivityGroup dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_UP)
然后执行ActivityGroup onUserInteraction
然后执行childActivity dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_UP)
然后执行childActivity onUserInteraction
然后执行childActivity onKeyUp


如果焦点在childView的listView上,按Back键的事件
首先执行ActivityGroup dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_DOWN)
然后执行ActivityGroup onUserInteraction
然后执行childActivity dispatchKeyEvent (event.getAction()==KeyEvent.ACTION_DOWN)
然后执行childActivity onUserInteraction 
然后执行childActivity onKeyDown
最后执行ActivityGroup onKeyDown

  Activity.dispatchKeyEvent(MotionEvent) - 这允许你的活动可以在分发给窗口之前捕获所有的键盘事件。
  由上面可知,当焦点在childView的listView上时,没有执行ActivityGroup onKeyDown,而有执行ActivityGroup的dispatchKeyEvent,因此可以在ActivityGroup的dispatchKeyEvent来捕获childActivity的Menu键事件

 

你可能感兴趣的:(android,nested,layer)