开发者

Android ViewGroup Remove Views?

I have a tabhost. One of the tab's activity is a ViewGroup. This viewgroup manages two different activities. I do this so I can navigate between activities within a tab. I add the activities like so:

if (videoViewLive == null)
            videoViewLive = getLocalActivityManager().startActivity("VideoPlayerLive", new Intent(this,VideoPlayerLive.class).
                    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();

        videoViewLive.setVisibility(View.VISIBLE);
        this.addContentView(videoViewLive, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));

Each of my content view activities receives asynchronous notifications. Wha开发者_开发百科t I would like to do is somehow remove the activity/content view that is not being used. So in essence, I load content view A, B dies, becomes null, or whatever, and vice versa. I want to do this because the way I am managing these views seems problematic. (errors when loading a view, loading the other view, then loading the first again, etc.)


Have you tried ViewGroup.removeAllViews()?


This is kind of tangential to the issue here, but why are you doing this with separate Activities?

This is exactly the kind of thing Fragments were designed for. There is actually a class called ViewPager included in the android support library (see also FragmentStatePagerAdapter) which allows the same kind of behavior via tabs (potentially in the ActionBar) or swiping. The adapter automagically handles the lifecycles of the Fragments as you navigate between them, all within the context of a single Activity, such that you can use the top-level Activity for routing events and maintaining overarching state if necessary.


I would try following approach:

  //Add OnGlobalLayout Listener using ViewTreeObserver

View rootView = (android.R.id.content);

//Assuming you are managing these two activities inside the ViewGroup
Activity activityA = <someRef Value>;
Activity activityB = <someRef Value>;

rootView.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
    //try couple of things here
    // 1. determine which activity has focus
    // 2. you could also check position of View on screen to determine which one is active or on the top 
    if (activityA.hasWindowFocus())
    { 
       //do some action --remove other content from ViewGroup
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜