Anil Avci
Anil Avci

Reputation: 11

How can I call Activity from fragment in android

I have two classes;

ProfileFragment.class

public class ProfileFragment extends Fragment implements OnClickListener {

    Fragment fragment = null;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_profile, container,false);
        return rootView;
    }
}

LastCheckins.class

public class LastCheckins extends Activity {    

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView sv = (ScrollView) findViewById(R.id.scrollView1);
        LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout1);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);
        for (int i = 0; i < 20; i++) {
            CheckBox cb = new CheckBox(this);
            cb.setText("I'm dynamic!");
            ll.addView(cb);
        }
        this.setContentView(sv);
    }
}

How can I call Activity class when the program create a ProfileFragment class?

Thanks a lot.

Upvotes: 1

Views: 156

Answers (2)

Stefan Shomodji
Stefan Shomodji

Reputation: 46

Use getActivity().

You can cast it to LastChekins so you can invoke its metods and access its visible fields:

((LastCheckins)getActivity()).methodX();

Upvotes: 2

FreshD
FreshD

Reputation: 3112

getActivity()

Returns the Activity this fragment is currently associated with.

Upvotes: 0

Related Questions