当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Fragment's view members null on config change, inside ViewPager

问题描述:

I have an activity with two swipeable fragments inside ViewPager.

I have an implementation of FragmentPagerAdapter which handles fragments in ViewPager.

The problem is that, I start an Intent(zxing barcode scanner) in fragment A, which calls onActivityResult() of parent activity(instead of fragment's, I tried many things but it won't work!).

Anyway, through Activity's onActivityResult() I'm fetching the fragment from ViewPager, calling a method on it which updates a searchview with scanner result. The fragment is fetched but the searchview which I want to update in Frag A is null(after configuration change, triggered by intent)

Here are my classes, some parts removed for simplicity:

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);

}

}

}

}

Acivity which hosts fragments:

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"));

}

}

}

Fragment 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);

}

}

网友答案:

Found it.

startActivityForResult(intent, code); should be called from Fragment in order to receive the result in Fragment. The above code does that via IntentIntegratorSupportV4, but I also had onActivityResult() in Activity which was preventing the call on Fragment's onActivityResult()

分享给朋友:
您可能感兴趣的文章:
随机阅读: