0

我有一个使用 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();
}


}
4

1 回答 1

1

该方法onActivityResult将在 MainActivity 和 Fragment 中调用,因此您不需要获取 Fragment。您必须在方法中使用不同的 RequestCode 来获取照片,startActivityForResult()并确保不要在 MainActivity 中使用该 RequestCode。

于 2013-05-12T02:50:42.823 回答