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) {
    if(index==0){
        Intent one=new Intent();
        one.setClass(MainActivity.this,FirstActivity.class);

    }else{
        Intent two=new Intent();
        two.setClass(MainActivity.this,SecondActivity.class);

    }

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

Answers


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

Search




Leave an answer