androidandroid-fragmentsandroid-actionbaractivity

How i can get ActionBarActivity from a Calss that extends from Fragment?


How i can get ActionBarActivity from a Class that extends from Fragment .

This is my class :

public class Email_Fragment extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBarActivity activity ;
        activity = this;

    }

    public Email_Fragment()
    {
        super();
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
    }

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

I need to this :

ActionBarActivity activity ;
activity = this;

And I need to use clicklistener .

Notice : I am using from import android.support.v7.app.ActionBarActivity;


Solution

  • In a fragment you can call getActivity() to get the "parent" Activity. If your fragment always lives inside of an ActiobarActivity you can cast it as follows:

    ActionBarActivity activity = (ActionBarActivity) getActivity();