Question

I'm adding a fragment to my activity inside oncreate of it. And on click of a button I have to add same fragment (the new data will be loaded and displayed in fragment) to the activity. Also i'm calling setHasOptionsMenu(true) after data is loaded. The data displayed in fragment has to be shared on click of share icon on actionbar.

Adding fragment in activity class:

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.add(R.id.myFragContainerLayout, myFrag);
    ft.addToBackStack(null);
    ft.commit();

Options menu in fragment :

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.share_menu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

Fragments are adding and i'm able to backstack the fragments without any problem but when I click on action item, the data which is displayed in the first fragment is getting shared. If I use below line (i.e replacing current fragment with other instead of adding) then everything is working fine but fragments are loading again on press of back. Where i'm going wrong? Please help me.

 ft.replace(R.id.myFragContainerLayout, myFrag);
Was it helpful?

Solution

onOptionsItenSelected() was returning return super.onOptionsItemSelected(item); Changed to return true; That's it. Fixed the issue

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top