如何在活动中调用片段类?

时间:2013-09-10 06:11:41

标签: android android-fragments

s2.java ..

public class S2 extends Activity implements OnClickListener,OnTouchListener

 {
...

 @Override

public void onBackPressed() 
    {
        ....
           //I want to call s1.java class here

   }


}

s1.java

public class S1 extends Fragment implements OnClickListener
{

...
}

现在我想调用s1.java,它扩展了s2.java onbackpress()中的片段。

2 个答案:

答案 0 :(得分:0)

试试这种方式

s2.java..

public class S2 extends FragmentActivity implements OnClickListener,OnTouchListener

{ ...

@Override

public void onBackPressed() { .... //I want to call s1.java class here
   S1 fragmentS1 = new S1();
   getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
}

}

s1.java

public class S1 extends Fragment implements OnClickListener {

... }

答案 1 :(得分:0)

如果要访问片段对象的实例,可以这样做:

getFragmentManager().findFragmentByTag(tag);

当您将某个片段添加到活动中时,可以为其设置TAG,如下所示:

getFragmentManager().beginTransaction().add(int containerViewId, Fragment fragment, String tag)