Search code examples
androidandroid-activityandroid-fragmentactivity

getActivity() with in FragmentActivity: android


I am using this class A which extends another abstract class (and this abstract class extends FragmentActivity) and in one of my function with in A class I want to get getActivity() for my current activity A. But whenever I use getActivity , It gives me error that getActivity() method is not defined type for my class. Please help me !! How can I achieve this??

Code for my class A

 public class A extends B
 {
  protected void onCreate(Bundle savedInstanceState) 
   {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.voicing);

    //doing another initializations and stuff//

   }
  }

code for class B

   public abstract class B extends FragmentActivity 
  {


   FragmentAdapter mAdapter;
   ViewPager mPager;
   PageIndicator mIndicator;


 }

Solution

  • A FragmentActivity is an instance of Activity (in Java style: FragmentActivity extends Activity).

    So there is no point calling getActivity because a FragmentActivity is already an Acivity, so just calling/using this will work. Also there is no method called getActivity in the FragmentActivity class. In other words your B class is extending an Activity instance.

    So inside your A (or B) class you could use this code snippet to get the activity instance:

    Activity test = (Activity) this;
    

    But if your B class extends Fragment then the getActivity call would have worked.