LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
 
Search this Thread
Old 03-02-2014, 11:22 PM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 444

Rep: Reputation: 30
android fragment position


I am trying to pass arguments to a fragment and then execute a function based on the arguments posted, in java/android using the examples from Google:

At first, I thought that the issue was that I am not executing onResume when the fragment is set visible, however I have also noticed that the numbers appear out of order.

Code:
public class MainActivity extends FragmentActivity
{
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
        {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }


    public class SectionsPagerAdapter extends FragmentPagerAdapter
        {

            public SectionsPagerAdapter(FragmentManager fm)
                {
                    super(fm);
                }

            @Override
            public Fragment getItem(int position)
                {
                    Fragment fragment = new DummySectionFragment();
                    Bundle args = new Bundle();

                    args.putInt("position", position + 1);
                    fragment.setArguments(args);

                    return fragment;
                }

            @Override
            public int getCount()
                {
                    return 3;
                }

            @Override
            public CharSequence getPageTitle(int position)
                {
                    Locale l = Locale.getDefault();
                    switch (position)
                        {
                        case 0:
                            return getString(R.string.title_section1).toUpperCase(l);
                        case 1:
                            return getString(R.string.title_section2).toUpperCase(l);
                        case 2:
                            return getString(R.string.title_section3).toUpperCase(l);
                        }
                    return null;
                }
        }

    public static class DummySectionFragment extends Fragment
        {
            private int position = -1;

            public DummySectionFragment()
                {
                }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
                {
                    View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);

                    position = getArguments().getInt("position");

                    return rootView;
                }

            @Override
            public void onResume()
                {
                    super.onResume();

                    Log.d("App", "Position: " + position);
                }
        }
}
Logcat:
Code:
03-05 09:44:53.653: D/App(23062): Position: 1
03-05 09:44:53.653: D/App(23062): Position: 2
03-05 09:45:02.473: D/App(23062): Position: 3
03-05 09:45:12.008: D/App(23062): Position: 1
03-05 09:45:13.353: D/App(23062): Position: 3
03-05 09:45:15.568: D/App(23062): Position: 1

I have looked at other posts but most of the resolutions appear to be to use multiple fragments i.e.

Code:
f = new Fragment1();
f = new FragmentOther();
f = new FragmentBobbityHead();
However this is not an option for me.

Last edited by trscookie; 03-04-2014 at 10:10 PM.
 
Old 03-03-2014, 09:58 PM   #2
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 444

Original Poster
Rep: Reputation: 30
......I'm wondering if its actually just not loading the log.d message when I flick between the fragments, how can I display a message when I flick through the fragments. I have tried using onResume and onStart, however none of them consistantly run when I focus on the fragment.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing UDP fragment acid_kewpie Linux - Networking 11 03-28-2010 10:22 AM
Code fragment explanation please! Completely Clueless Programming 14 10-30-2009 10:15 AM
Does EXT2/# Fragment? sirclown82 General 6 03-13-2007 08:51 PM
Any use for fragment shader HW under Linux? randomshinichi Linux - Software 0 09-28-2005 08:41 AM
Suspect TCP fragment aimstr8 Linux - Networking 0 10-23-2002 10:18 AM


All times are GMT -5. The time now is 02:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration