Activity 跳转 Fragment

        Activity 是不能够直接跳转到Fragment的 只能 先跳转到Fragment所挂载的Activity上面 然后在跳转的同时 Activity可以携带一个参数 作为标记,然后在对Fragemnt的宿主Activity设置去显示那个Fragment。 (nTabHost.setCurrentTab(0));

        上代码:

a.需要跳转的页面 用Intent 携带参数

 intent.setClass(SendAlertThirdStepActivity.this,MainActivity.class);
            intent.putExtra("extra", "1");
            startActivity(intent);

b.Fragment的宿主Activity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mLoginPresenter = new LoginPresenter(this, this);

        mCheckUpatePresenter = new ChechUpatePresenter(this, this);

        mLoginPresenter.doLogin();

          <strong>  Intent intent=getIntent();
            Value=intent.getStringExtra("extra");
        if(Value==null){
            tag = 0;
        }else{
            tag = 1;
        }</strong>
        mCheckUpatePresenter.doCheckUpdate();
        initViews();
    }
    private void initViews() {
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        mTabHost.getTabWidget().setDividerDrawable(null);

        mTabHost.addTab(
                mTabHost.newTabSpec(TAG_MC_FRAGMENT).setIndicator(createTabIndicatorView(R.string.lost_child, R.drawable.tab_lost)),
                MissingChildrenFragment.class, null);

        mTabHost.addTab(
                mTabHost.newTabSpec(TAG_WR_FRAGMENT).setIndicator(createTabIndicatorView(R.string.alert, R.drawable.tab_alert)),
                WarningFragment.class, null);

        mTabHost.addTab(
                mTabHost.newTabSpec(TAG_ST_FRAGMENT).setIndicator(createTabIndicatorView(R.string.mine, R.drawable.tab_myself)),
                SettingFragment.class, null);

      <strong>  if(tag==0){
            mTabHost.setCurrentTab(0);
        }else {
            mTabHost.setCurrentTab(1);
        }</strong>
    }


值得注意的是一定要先initViews() 再去设置 mTabHost.setCurrentTable()的值 要不然 崩溃

mTabHost.setCurrentTab(0);

你可能感兴趣的:(Activity 跳转 Fragment)