0

I saw a code here. Also shown below:

I'm in bit confused in Fragment fragment = new DemoObjectFragment(); where public static class DemoObjectFragment extends Fragment is a static class. So how we can call fragment.setArguments(args);

public class CollectionDemoActivity extends FragmentActivity {
    // When requested, this adapter returns a DemoObjectFragment,
    // representing an object in the collection.
    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection_demo);

        // ViewPager and its adapters use support library
        // fragments, so use getSupportFragmentManager.
        mDemoCollectionPagerAdapter =
                new DemoCollectionPagerAdapter(
                        getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
    }
}

// Since this is an object collection, use a FragmentStatePagerAdapter,
// and NOT a FragmentPagerAdapter.
public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DemoObjectFragment();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}

// Instances of this class are fragments representing a single
// object in our collection.
public static class DemoObjectFragment extends Fragment {
    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = inflater.inflate(
                R.layout.fragment_collection_object, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                Integer.toString(args.getInt(ARG_OBJECT)));
        return rootView;
    }
}
Chris J
  • 949
  • 9
  • 15
  • Are you asking what a `static` class is? – Sotirios Delimanolis Sep 28 '13 at 17:37
  • I think, we can't create an instance of a static class. And if we want to access static class members, we call using CLASSNAME.MEMBER – Chris J Sep 28 '13 at 17:38
  • 3
    Both of those statements are wrong (or just the first depending on how you meant the second one). Read this tutorial: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – Sotirios Delimanolis Sep 28 '13 at 17:39
  • But it is a nested class. But here, it is Fragment class. Is Fragment is a nested class of any other class? – Chris J Sep 28 '13 at 17:42
  • look at this : http://stackoverflow.com/questions/3584113/why-are-you-not-able-to-declare-a-class-as-static-in-java – Tourki Sep 28 '13 at 20:00
  • @YAT You are correct. But my question is, the class `Fragment` or subclass of `Fragment` is nested class of any other class ??? – Chris J Sep 29 '13 at 05:46

1 Answers1

0

I created a base class for using fragments. I don't know if that will work for you. In a base class you can put information that's the same for all fragments. In my case a want to use a shared view model in all fragments. So I put the shared view model in the base class :

/**
 * base class for the application
 */
public abstract class BaseFragment extends Fragment {

    // use shared view model for exchanging information between fragments.
    SharedViewModel sharedViewModel;

    /**
     * this function creates view and shared view model
     * @param inflater - inflater creates the view
     * @param container - container with views
     * @param savedInstanceState - for saving state
     * @return - return view
     */
     @Nullable
     @Override
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
         ViewGroup container, @Nullable Bundle savedInstanceState) {

         // call base class first
         super.onCreateView(inflater, container, savedInstanceState);

         // then create view
         View fragView = inflater.inflate(getFragment(), container, false);

         // get shared view model.
         sharedViewModel = new 
             ViewModelProvider(requireActivity()).get(SharedViewModel.class);

         // inject fragment depended code
         createView(fragView);

         // return created view
         return fragView;
  }

  /**
   * get fragment
   * (override in fragments and return xml layout name)
   */
   protected abstract int getFragment();

   /**
    * inject fragment depended code while creating the view
    * (override in fragments with fragment depended code)
    */
    protected abstract void createView(View fragView);
}
  • Sir, IT (question I asked) WAS A MISTAKE FROM https://developer.android.com/ They correct it in the site ifself as --> public class DemoObjectFragment extends Fragment. It is NOT STATIC – Chris J Jun 16 '22 at 07:14