public class MyActivity extends FragmentActivity { // ... private void closeActivity() { finish(); } // ... }
public class MyFragment extends Fragment { // ... private void closeActivity() { FragmentActivity activity = getActivity(); if (activity != null) { activity.finish(); } } // ... }In this example, the closeActivity() method is called from a fragment to close the parent activity. Since a fragment doesn't have a finish() method, we need to get a reference to the parent activity using the getActivity() method. The android.support.v4.app package is a support library that provides additional functionality for Android apps. It includes classes and methods that are not available in older versions of Android, as well as compatibility features to make it easier to write apps that work across multiple versions of Android.