当前位置: 代码迷 >> Android >> android2.2的LocalActivityManager的destroyActivity()有关问题
  详细解决方案

android2.2的LocalActivityManager的destroyActivity()有关问题

热度:244   发布时间:2016-05-01 12:48:13.0
android2.2的LocalActivityManager的destroyActivity()问题
   

      今天在用ActivityGroup做例子,发现了个问题,并采用了个笨办法暂时解决。

      首先看LocalActivityManager的destroyActivity()的源码:

     

/**     * Destroy the activity associated with a particular id.  This activity     * will go through the normal lifecycle events and fine onDestroy(), and     * then the id removed from the group.     *      * @param id Unique identifier of the activity to be destroyed     * @param finish If true, this activity will be finished, so its id and     * all state are removed from the group.     *      * @return Returns the window that was used to display the activity, or     * null if there was none.     */    public Window destroyActivity(String id, boolean finish) {        LocalActivityRecord r = mActivities.get(id);        Window win = null;        if (r != null) {            win = performDestroy(r, finish);            if (finish) {                mActivities.remove(r);            }        }        return win;    }


        大家可能已经发现remove的时候,错误,应该是remove(id)。不知道google的错误,还是故意这么做,不让删除。

        这样以来在调用destroyActivity就没法删除,这样在做ActivityGroup的返回就会出错误。

        具体ActivityGroup中Activity的返回代码如下:

       

@Override	public boolean onKeyDown(int keyCode, KeyEvent event) {				if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP){			onBackPressed();		}		return super.onKeyDown(keyCode, event);	}	public void onBackPressed() {		if(index>1){			activityManager.destroyActivity((index)+"", true);			boolean isWhile = true;			while(isWhile){				Activity activity = activityManager.getActivity((--index)+"");				if(activity!=null){					startActivityBack(activity);					isWhile = false;					break;				}			}		}else{			super.onBackPressed();		}	}


        一下是在ActivityGroup中start和back调用的方法:

       

private int index = 0;		public void startActivityForGroup(Intent intent) {		boolean isWhile = true;		while (isWhile) {			Window window = activityManager.startActivity(					(++index)+"", intent);			Log.d(TAG, "==window:"+window);			Log.d(TAG, "==CurrentId:"+index);			if(window!=null){				isWhile = false;				View decorView =window.getDecorView();				contentView.removeAllViews();				contentView.addView(decorView);				break;			}		}	}		public void startActivityBack(Activity activity){		if(activity.getWindow()==null)			return;		System.out.println("==view:"+activity.getWindow().getDecorView());		contentView.removeAllViews();		contentView.addView(activity.getWindow().getDecorView());			}

          这样,暂时能够保持正常返回。但是存在隐患。希望大家能够交互方法,让我们的代码更加完善。