1

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();


        }


    }

Grumpy Cat
  • 1,219
  • 2
  • 11
  • 17
  • Would be much easier if you add a view pager page change listener that sets the action bar title. It is much easier to both listen and get the action bar there. – SOFe Aug 10 '16 at 19:03
  • can you tell me how to do it, i am new to android – Grumpy Cat Aug 10 '16 at 19:04
  • @GrumpyCat see here http://stackoverflow.com/questions/11293300/determine-when-a-viewpager-changes-pages – sswierczek Aug 10 '16 at 19:05
  • Set a ViewPager.SimpleOnPageChangeListener, implement onPageSelected, and use your own way (because it depends on your implementation) to get the instance of the fragment for that index and get the title. – SOFe Aug 10 '16 at 19:08
  • Hello guys it work using ViewPager simpleOnPAgeChangeListener, Thanks for your help – Grumpy Cat Aug 10 '16 at 19:54

2 Answers2

0

it is because viewpager loads onCreateView method of next fragment after calling it on current fragment, and you can't set viewPagerOffset limit to 0. you can change your action bar title from your fragment by this code :

@Override
public void setUserVisibleHint(boolean isVisibleToUser) { 
    super.setUserVisibleHint(isVisibleToUser);
    m_iAmVisible = isVisibleToUser;
    if (m_iAmVisible) { 
        Log.d(localTAG, "this fragment is now visible");
         //change your actionbartitle
    } else {  
        Log.d(localTAG, "this fragment is now invisible");
    }
}
Reza.Abedini
  • 2,227
  • 2
  • 16
  • 18
  • i try like this : public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); boolean m_iAmVisible = isVisibleToUser; if (m_iAmVisible) { ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(name); } else { } } but get error Attempt to invoke virtual method 'android.support.v7.app.ActionBar android.support.v7.app.AppCompatActivity.getSupportActionBar()' on a null object reference – Grumpy Cat Aug 10 '16 at 19:23
  • is your activity extends FragmentActivity? – Reza.Abedini Aug 10 '16 at 19:37
  • No, I am using view pager, i extend fragment in MyFragment class – Grumpy Cat Aug 10 '16 at 19:39
  • i mean parent activity that you put your viewpager there. – Reza.Abedini Aug 10 '16 at 19:43
  • here is my parent activity : public class ItemFragment extends AppCompatActivity { } – Grumpy Cat Aug 10 '16 at 19:49
  • you can create a public method in your parent activity that changes your actionbar title.and then you can call it from your fragment like this : ((ItemFragment )getActivity()).yourMethodName("your title"); – Reza.Abedini Aug 10 '16 at 19:55
  • Hello Reza, My problem get solved using onPagechange listener, I called it in my parent activity, thanks for your help – Grumpy Cat Aug 10 '16 at 19:59
0

but it set name of next item's name in actionbar.

By default, ViewPager Adapter will create the current and the next fragment view for you, refer to:

Book "Android.Programming.The.Big.Nerd.Ranch.Guide" P207 enter image description here

Life cycle methods:

// when first in page0
D/Adapter (25946): getItem(0)
D/Adapter (25946): getItem(1)
D/Fragment1(25946): newInstance(Hello World, I'm li2.)
D/Fragment0(25946): onAttach()
D/Fragment0(25946): onCreate()
D/Fragment0(25946): onCreateView()
D/Fragment1(25946): onAttach()
D/Fragment1(25946): onCreate()
D/Fragment1(25946): onCreateView() // that's why it set name of next item.

So it would be better to update ActionBar title in Activity:

    // init title
    mViewPager.setCurrentItem(i);

    // update title
    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageSelected(int pos) {
            setTitle(...);
        }
    });
Weiyi
  • 1,843
  • 2
  • 22
  • 34