Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

android - refresh a fragment from its parent activity

I have almost the exact same problem as this question here: Android, How to restart/refresh a fragment from FragmentActivty?

I'm trying to call the a method of a ListFragment from the parent FragmentActivity.

However, I'm using the template Swipe + Fixed Tabs generated by eclipse. I tried calling getSupportFragmentManager().findFragmentById(R.id.myfragment) but the return value is always null. I'm guessing this might be a problem because I did not define myfragment anywhere in my app. But I am unsure where to define it as all fragments are created dynamically.

For those who are not familiar with the Swipe + Fixed Tabs template generated by the Android SDK in Eclipse, the fragments are created by overriding the FragmentPagerAdapter getItem function, returning a new instance of my fragment.

Any help would be appreciated.

Relevant code: How I setup my adapter:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.activity_comment);
mViewPager.setAdapter(mSectionsPagerAdapter);

Overriding the adapter getItem function:

@Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.

        switch(position) {
        case 0:
            return CommentsFragment.newInstance();

        case 1:
        default:
            return LikesFragment.newInstance();
        }
    }

The newInstance() functions simply return an instance of themselves, since the classes for my fragment are static.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to do this :

frag.getFragmentManager().beginTransaction().detach(frag).commit();
frag.getFragmentManager().beginTransaction().attach(frag).commit();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...