近来在调试imsdroid,想限制转屏。结果发现常用的办法无效。这是为什么呢?后来研究了一下启动Activity的代码,有点独特。具体代码是:
@Override public boolean show(Class<? extends Activity> cls, String id) { final Main mainActivity = (Main)Engine.getInstance().getMainActivity(); String screen_id = (id == null) ? cls.getCanonicalName() : id; Intent intent = new Intent(mainActivity, cls); intent.putExtra("id", screen_id); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); final Window window = mainActivity.getLocalActivityManager().startActivity(screen_id, intent); if(window != null){ View view = mainActivity.getLocalActivityManager().startActivity(screen_id, intent).getDecorView(); LinearLayout layout = (LinearLayout) mainActivity.findViewById(R.id.main_linearLayout_principal); layout.removeAllViews(); layout.addView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // add to stack this.mLastScreens[(++this.mLastScreensIndex % this.mLastScreens.length)] = screen_id; this.mLastScreensIndex %= this.mLastScreens.length; return true; } return false; }
初步分析来说,相当于把一个Activity作为容器,然后把另外一个Activity放进去。也就是说,要想设置,就必须在主Activity中(或者在Manifest.xml)。