K
K
KnightForce2015-03-16 14:40:32
Android
KnightForce, 2015-03-16 14:40:32

What does the line of code "protected abstract Fragment createFragment();" do?

There is code:
public abstract class SingleFragmentActivity extends FragmentActivity {
protected abstract Fragment createFragment();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
I understand what it does except for one line:
protected abstract Fragment createFragment();
protected abstract - it's clear here, protected abstract.
Fragment - I, as I understand it, type.
createFragment() is also understandable.
But together it's completely unclear to me.
It would be like this:
protected abstract Fragment fm = createFragment();
I would understand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2015-03-16
@onepavel

You should look through the java tutorial =)
you have an abstract class SingleFragmentActivity, which cannot have objects.
can only be inherited from it.
and the line "protected abstract Fragment createFragment();" tells the programmer
that I am declaring a createFragment() method with no implementation.
when you inherit from a class, be sure to write code for this method
in the future your implementation will be used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question