我有一个使用 Tab 导航、ViewPager 和 ActionBarSherlock 的应用程序。在我的一个片段中,我使用意图获得了一张照片,但onActivityResult
在 MainActivity 而不是片段中调用。我想在 MainActivity 中获取片段onActivityResult
,然后调用片段onActivityResult
。
我整晚都在尝试,但我不知道如何获得片段。
基本上我想做这样的事情,但我无法得到片段:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag("fragment");
if(fragment != null){
fragment.onActivityResult(requestCode, resultCode, data);
}
}
编辑:
选项卡适配器
public class TabsAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener , ViewPager.OnPageChangeListener{
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.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 onPageScrollStateChanged(int state) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.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 void onTabUnselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return android.support.v4.app.Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public int getCount() {
return mTabs.size();
}
}