I am using I am using view pager and fragments in my project, I am trying to set title of Actionbar from fragment class. My following code showing title of next fragment on current visible fragment. can you have any ideas how to show current visible fragments title in Action Bar
public class MyFragment extends Fragment {
TextView textView;
int mCurrentPage;
String name = null, data = null;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setData();
View view = inflater.inflate(R.layout.fragment_Item, container, false);
textView = (TextView) view.findViewById(R.id.fragmentItemTextView);
textView.setText(data);
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(name);
//this sets title of actionbar
//but it set name of next item's name in actionbar
return view;
}
void setData() {
//here I call database to get name of item and data of item
ItemProcess sp = new ItemProcess(getActivity().getApplicationContext());
ArrayList temp;
temp = sp.getSingleItem(mCurrentPage);
name = temp.get(0).getmItemName();
data = temp.get(0).getmItemData();
}
}
