在ViewPager中,Fragment的视图成员在配置更改时为null

时间:2014-01-23 19:19:27

标签: java android android-fragments android-activity zxing

我在ViewPager内有两个可滑动片段的活动。 我有FragmentPagerAdapter的实现,它处理ViewPager中的片段。 问题是,我在片段A中启动一个Intent(zxing条形码扫描器),调用父活动的onActivityResult()(而不是片段,我尝试了很多东西,但它不起作用!)。

无论如何,通过Activity的onActivityResult()我从ViewPager获取片段,在其上调用一个方法,用扫描器结果更新搜索视图。提取片段但我想在Frag A中更新的搜索视图为空(在配置更改后,由intent触发)

以下是我的课程,为简单起见删除了一些部分:

public class TabsAdapter extends FragmentPagerAdapter {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
    private final String TAG = "21st Polling:";

    static final class TabInfo{
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args){
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity fa, ViewPager pager) {
        super(fa.getSupportFragmentManager());
        mContext = fa;
        mActionBar = fa.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args){
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
        Log.v(TAG, "clicked");
        Object tag = tab.getTag();
        for (int i = 0; i<mTabs.size(); i++){
            if (mTabs.get(i) == tag){
                mViewPager.setCurrentItem(i);
            }
        }

    }
}

主持片段的活动:

public class OrderActivity extends SherlockFragmentActivity implements
        OrderData {

    private ViewPager mViewPager;
    private TabsAdapter mTabsAdapter;
    private Tab cartTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);
        setContentView(mViewPager);

    final ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mTabsAdapter = new TabsAdapter(this, mViewPager);
    mTabsAdapter.addTab(bar.newTab().setText("A"),
            A.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("B(0)"),
            B.class, null);

    }


    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        IntentResult scanResult = IntentIntegrator.parseActivityResult(
                requestCode, resultCode, intent);
        //calling method productScaned() in fragment A here 
        if (scanResult != null) {
             ((A)mTabsAdapter.getItem(0)).productScaned(intent.getStringExtra("SCAN_RESULT"));
        }
    }

}

片段A:

public class A extends SherlockFragment {

    private SearchView svMProducts;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a, container,
                false);
        svMProducts = (SearchView) view.findViewById(R.id.svMProductsInvoice);
        svMProducts.setOnQueryTextListener(queryListener);
        svMProducts.requestFocus();

        btnScanBarcode = (ImageView)view.findViewById(R.id.btnScanBarcode);
        btnScanBarcode.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                                //tried with IntentIntegrator also, didn't work
                IntentIntegratorSupportV4 integrator = new IntentIntegratorSupportV4(A.this);                         
                integrator.initiateScan();
            }
        });
        return view;
    }

    public void productScaned(String upc){
                //this method gets called but svMProducts view is null!
        svMProducts.setQuery(upc, true);
    }
}

1 个答案:

答案 0 :(得分:0)

找到它。

应从Fragment调用

startActivityForResult(intent, code);以便在Fragment中接收结果。上面的代码是通过IntentIntegratorSupportV4执行的,但我在onActivityResult()中也有Activity,这阻止了对Fragment onActivityResult()的调用