Andriod 设计制作切换包含不同的Antivity

1.布局:

利用RadioGroup布局,每个radio可以设计为图片,点击radio,包含进来不同的Activity

布局里还有个视图容器

2.后台


public abstract class StackActivityGroup extends ActivityGroup {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mManager = getLocalActivityManager();
    }


    public void startSubActivity(Class<?> which, Bundle param) {
        Intent intent = new Intent();
        intent.setClassName(this.getPackageName(), which.getName());
        Log.i("main1", this.getPackageName());//com.icson
        Log.i("main1", which.getName());//com.icson.home.HomeActivity
        if (param != null) {
            intent.putExtras(param);
            //message must clear_top otherwise will not call onNewIntent
            if(param.containsKey(MsgEntity.SERIAL_NAME_MSGENTITY))
            {
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            }
        }
        startSubActivity(intent);
    }

    public void startSubActivity(Intent intent) {
        final String strClassName = intent.getComponent().getClassName();//com.icson.home.HomeActivity
        if (mContainer == null) {
            mContainer = getContainer();

        }
        
        // Check whether the activity already exists.
        View pDecorView = mManager.startActivity(strClassName, intent).getDecorView();//getDecorView 获取顶级视图
        if( null != pDecorView && null != mContainer )
        {
            mContainer.removeAllViews();
            mContainer.addView(pDecorView);
        }
    }


LocalActivityManager statrtActivity 解释

理解:这里的current activity指的应该是哪个ID相同的activity.
public Window startActivity (String id, Intent intent)
Added in API level 1

Start a new activity running in the group. Every activity you start must have a unique string ID associated with it -- this is used to keep track of the activity, so that if you later call startActivity() again on it the same activity object will be retained.

When there had previously been an activity started under this id, it may either be destroyed and a new one started, or the current one re-used, based on these conditions, in order:

  • If the Intent maps to a different activity component than is currently running, the current activity is finished and a new one started.
    • 理解:如果Intent导向的是相同ID的activity componet是不同的,而不是当前正在运行的那个current activity
    • 则current activity将被干掉并且启动个新的

  • If the current activity uses a non-multiple launch mode (such as singleTop), or the Intent has the FLAG_ACTIVITY_SINGLE_TOP flag set, then the current activity will remain running and its Activity.onNewIntent() method called.
  • If the new Intent is the same (excluding extras) as the previous one, and the new Intent does not have the FLAG_ACTIVITY_CLEAR_TOP set, then the current activity will remain running as-is.
  • Otherwise, the current activity will be finished and a new one started.

If the given Intent can not be resolved to an available Activity, this method throws ActivityNotFoundException.

Warning: There is an issue where, if the Intent does not include an explicit component, we can restore the state for a different activity class than was previously running when the state was saved (if the set of available activities changes between those points).

Parameters
idUnique identifier of the activity to be started
intentThe Intent describing the activity to be started
Returns
  • Returns the window of the activity. The caller needs to take care of adding this window to a view hierarchy, and likewise dealing with removing the old window if the activity has changed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值