fragment添加menu 以及 fragment中调用onActivityResult

工作中,经常能遇到,Activity中放ViewPager,ViewPager里面放几个fragment来切换,然后Activity中的菜单项需要能够更改fragment里面的信息。

整理出如下代码:

1.Activity代码如下:

public class AAActivity extends BaseActivity{

	private ViewPager viewPager;
	private AgentMomentPagerAdapter pagerAdapter;

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

		setContentView(R.layout.activity_agent_moment);
		viewPager = (ViewPager) findViewById(R.id.pager_agent_moment);

		pagerAdapter = new PagerAdapter(this, getSupportFragmentManager());
		viewPager.setAdapter(pagerAdapter);
		viewPager.setCurrentItem(0);
		viewPager.setOffscreenPageLimit(0);
		
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(this.getString(R.string.search))
		.setIcon(R.drawable.ic_search);
		MenuItemCompat.setShowAsAction(menu.getItem(0), MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
		
		menu.add(this.getString(R.string.send))
		.setIcon(R.drawable.ic_send);
		MenuItemCompat.setShowAsAction(menu.getItem(1), MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		String title = item.getTitle().toString();
		if(title.equals(getString(R.string.search))){
			Intent it = new Intent(AAActivity.this,a0.class);
			startActivity(it);
		}else if(title.equals(getString(R.string.send))){
			Intent intent = new Intent(AAActivity.this, a1.class);
			startActivity(intent);
		}else
			return super.onOptionsItemSelected(item);//这句也要,不然左上角那个返回键不会退出当前页面
		
		return false;
	}

	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data)  {  
		super.onActivityResult(requestCode, resultCode, data);//这里一定要,不然代码走不到fragment中的onActivityResult
	}
}

2.fragment代码如下:


public class AAFragment extends Fragment {

	public static AAFragment newInstance() {
		AAFragment fragment = new AAFragment();
		return fragment;
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);//importent
    }
	
	@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        menu.add(this.getString(R.string.more))
		.setIcon(R.drawable.ic_more);
		MenuItemCompat.setShowAsAction(menu.getItem(2), MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
    }
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)  {  

		super.onActivityResult(requestCode, resultCode, data);
			if(requestCode == 11){
				//do something
			}

	}
      
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
    	if(item.getTitle().equals(getString(R.string.more))){
    		Intent intt = new Intent(getActivity(),ActionbarNoPicDialogActivity.class);
    		startActivityForResult(intt,11);/注意:这里不能使用getActivity().startActivityForResult()接口
    		return true;
    	}
        return super.onOptionsItemSelected(item);
    }

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_agent_moment_list, null);
		
		return rootView;
	}
}

3.PagerAdapter代码如下:

public class PagerAdapter extends FragmentPagerAdapter {
	private Fragment[] fragments;
	private Integer[] titles;
	private Context mCtx;

	public PagerAdapter(Context ctx, FragmentManager fm) {
		super(fm);		
		this.mCtx = ctx;
		fragments = new Fragment[2];
		fragments[0] = AAFragment.newInstance();
		fragments[1] = ABFragment.newInstance();
		titles = new Integer[]{R.string.string_all, R.string.string_little};
	}

	@Override
	public Fragment getItem(int position) {
		// TODO Auto-generated method stub
		return fragments[position];
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return fragments.length;
	}
	
	public int getTittle(int position){
		return titles[position];
	}
	
	@Override  
    public CharSequence getPageTitle(int position){
         return mCtx.getText(titles[position]);
    }

}

总结:

fragment添加menu最重要的是在fragment中的OnCreate()中使用setHasOptionsMenu(true);

fragment使用onActivityResult()时最重要是在Activity中使用super.onActivityResult(),




你可能感兴趣的:(android)