在onNewIntent不工作后重新创建片段

时间:2015-01-12 12:48:40

标签: android android-fragments

我在我的活动的Fragment中重新创建onNewIntent时遇到问题:

public class MessageActivity extends NavigationActivity {
    private FragmentManager fragmentManager = null;
    private MessageFragment fragment = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.fragmentManager = this.getSupportFragmentManager();
        this.fragment = (MessageFragment) this.fragmentManager.findFragmentByTag(FragmentTags.MESSAGE.getValue());

        if (savedInstanceState == null) {
            this.fragment = new MessageFragment();
            this.fragmentManager.beginTransaction().replace(R.id.menu_slide_main_content, this.fragment, FragmentTags.MESSAGE.getValue()).commit();
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        this.setIntent(intent);
        this.fragmentManager = this.getSupportFragmentManager();
        this.fragment = (MessageFragment) this.fragmentManager.findFragmentByTag(FragmentTags.MESSAGE.getValue());

        if(fragment == null) {
            this.fragment = new MessageFragment();
        }

        this.fragmentManager.beginTransaction().replace(R.id.menu_slide_main_content, this.fragment, FragmentTags.MESSAGE.getValue()).commit();
    }
}

我尝试重新创建的FragmentSlidingTabLayoutViewPager

public class MessageFragment extends Fragment {
    private SlidingTabLayout slidingTabLayout;
    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_message_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.viewPager = (ViewPager) view.findViewById(R.id.messages_pager);
        this.viewPager.setId(0x5000);
        this.viewPager.setAdapter(new MessagePagerAdapter(this.getActivity(), this.getFragmentManager()));
        this.slidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.messages_tab_layout);
        this.slidingTabLayout.setViewPager(this.viewPager);
    }
}

现在调用onNewIntent并重新创建Fragment我的片段中的片段 SlidingTabLayout是空的,滑动不再起作用。

我该如何解决这个问题?

0 个答案:

没有答案