FragmentActivity会创建多个Fragments导致问题

时间:2013-06-17 09:07:07

标签: android android-fragments android-fragmentactivity

我实例化一个FragmentActivity(FA1),它在其片段(F1)中显示文章(使用ViewPager)。每篇文章片段都有一个额外的类(W)实例,一个WebManager,它保存文章的ID。如果我单击文章中的图像,WebViewClient将创建另一个FragmentActivity(FA2)(文章ID保存在包中),在其中显示图像的片段(F2)。

发生以下情况:

FA1建立三个F1:A3,A2,A1(按此顺序)。每个F1用它的ID实例化一个W(A3:ID = A3,A2:ID = A2,A1:ID = A1)。此外,ID被覆盖,每次都是A3。如果我点击A2中的图像,W将创建ID = A3的FA2,而不是A2!

我可以做些什么来保存这个F1的W实例,我点击了? 不知道该怎么做:(

希望我说明方案正确(英文),在german forum中也提出这个问题......

- 编辑 -

F1:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
...
    articleID = args.getString("articleID");//set in FA1, will be overwriten
    WebView webView = ((WebView) rootView.findViewById(R.id.articleFragment));
    WebManagerArticle wm = WebManagerArticle.getInstance(activity, Integer.parseInt(articleID));
    webView.setHttpAuthUsernamePassword(Constants.HOST, "", Constants.HTTP_USER, Constants.HTTP_PASSWORD);
    webView.setWebViewClient(wm.getArticleWebViewClient());
...
return rootView;
}

WebManagerArticle:

public class WebManagerArticle extends WebManager
{
    private static WebManagerArticle instance = null;
    private static int id = -1;
    private static ArticleWebViewClient webViewClient = null;
    private WebManagerArticle()
    {
        super();
        webViewClient = new ArticleWebViewClient();
    }

    public static WebManagerArticle getInstance(Activity a, int articleId)
    {
        if (instance == null)
        {
            instance = new WebManagerArticle();
        }
        activity = a;
        id = articleId;
        return instance;
    }

    public ArticleWebViewClient getArticleWebViewClient(){return webViewClient;}

    private class ArticleWebViewClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            ...//HERE
            Intent intent = new Intent(activity, ImagePageSliderActivity.class);
            intent.putExtra("imagePath", imagePath); //get imagePath from url with pattern matching
            intent.putExtra("articleID", id);
            activity.startActivity(intent);
            ...
            return true;
        }
        @Override
        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm)
        {
            handler.proceed(Constants.HTTP_USER, Constants.HTTP_PASSWORD);
        }
    }
}

ImagePageSlideActivity

public class ImagePageSliderActivity extends SherlockFragmentActivity
{
    public Context context;
    public int articleID;
    public int pages;
    private int startPosition;
    public ArrayList<String> imagePaths;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        context = this;
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_article_page_slider);

        String startImagePath = getIntent().getStringExtra("imagePath");
        // here's the wrong article ID!!!
        articleID = getIntent().getIntExtra("articleID", -1);

        // pages get From Database
        // startPosition get From Database
        // imagePaths get from database

        viewPager = (ViewPager) findViewById(R.id.articlePager);
        PagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(
                getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(startPosition);
    }

    @Override
    public void onBackPressed()
    {
        if (viewPager.getCurrentItem() == startPosition)
        {
            super.onBackPressed();
        } else if (viewPager.getCurrentItem() > startPosition)
        {
            viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
        } else if (viewPager.getCurrentItem() < startPosition)
        {
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
        }
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
    {
        public ScreenSlidePagerAdapter(FragmentManager fm){super(fm);}

        @Override
        public Fragment getItem(int position)
        {
            Fragment fragment = new ImagePageFragment();

            Bundle args = new Bundle();
            args.putString("imagePath", imagePaths.get(position));

            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount()
        {
            return pages;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

不要让WebManagerArticle成为单身人士,也不要只使其字段为static,因为他们会分享最后Fragment个已初始化的状态。当ViewPager创建第一个页面片段时,将创建WebManagerArticle以及正确的ID。构建第二个页面片段时,WebManagerArticle已经构建,并且在其上设置字段将覆盖为第一个片段存储的先前数据。对于最后一个片段也是如此,这就是为什么你得到第三个片段的唯一id。