Home Ask Login Register

Developers Planet

Your answer is one click away!

Navod Neranjan February 2016

How do i call my fragment class in android?

I am in my MainActivity.java class and I need to call for My Fragments inside MainActivity according to User click List View item.Is there a any way to do that like starting a activity using intent for calling a fragment inside in MainActivity class?

 private void onListItemClicked(int index) {
        Intent one=new Intent();

        Intent two=new Intent();


Here is my code.For this one i use starting a another activity.But i need to start calling fragment..Is there any way?


Ajinkya February 2016

for adding fragment to activity

Fragment newFragment = new Fragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(CONTENT_VIEW_ID, newFragment).commit();

where CONTENT_VIEW_ID is id of container in which you want to add fragment.

Post Status

Asked in February 2016
Viewed 1,019 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life