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

179 views Asked by At

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;

2

There are 2 answers

0
Florian Barth On BEST ANSWER

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();
0
Tooto On

You can get link to activity via getActivity() and cast it.

Read https://developer.android.com/guide/components/fragments.html

I guess, "Communicating with the Activity" will help for you.