Skip to content

Usage for Fragments

Marcin Moskała edited this page Jul 2, 2017 · 4 revisions

Usage for Fragments is pretty the same as [Activities](Usage for Activities) with following differences:

  • In generated class, there is no method for Fragments start. Only newInstance, which is standard way to provide Fragment intent.
  • Arguments also need to be filled, but there is no onCreate method. In standard way it done on onCreateView, like in following code:
public class TabbedPlaceholderFragment extends Fragment {

    @Arg int sectionNumber;

    public TabbedPlaceholderFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tabbed, container, false);
        ActivityStarter.fill(this);
        // ...
        return rootView;
    }
}

If you want to save instance state use following code:

public class TabbedPlaceholderFragment extends Fragment {

    @Arg int sectionNumber;

    public TabbedPlaceholderFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tabbed, container, false);
        ActivityStarter.fill(this, savedInstanceState);
        // ...
        return rootView;
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        ActivityStarter.save(this, outState)
    }
}

0.60-beta.2

It also can be done in BaseFragment.

Clone this wiki locally