Hello,
So in the first part of the chapter, we learn how to move to an Activity from a Fragment, which is pretty straightforward. But I do not understand how we get a CrimeFragment when we transit to CrimeActivity (I know this question is a bit old for the chapter but still not too late).
So, this part of CrimeListFragment is the source of the transition which I have no issues with:
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), CrimeActivity.class);
startActivity(intent);
}
and CrimeActivity looks something like this:
import android.support.v4.app.Fragment;
public class CrimeActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return new CrimeFragment();
}
}
And the SingleFragmentActivity abstract class looks like this:
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if(fragment == null){
fragment = createFragment();
fm.beginTransaction().add(R.id.fragment_container,fragment).commit();
}
}
}
So, here are my quesitons. In CrimeActivity why do we only Override this method called “createFragment()” which only returns a new Fragment. Why dont we have a function “onCreate()”? I am not an expert in Java but I would be more than grateful if someone could explain the transition process and the use of abstract class. Thanks a lot!