在这里,我的应用程序有像这样的活动
public class ticket extends FragmentActivity
我的应用程序有像这样的片段
public class TicketFragment extends Fragment
我想从故障单活动中调用TicketFragment类。如何调用此片段???请帮助..
答案 0 :(得分:0)
在片段中,您可以使用getActivity()访问其父FragmentActivity实例。 由于该实例是一项活动,因此使用它可以简单地调用另一项活动。
Intent myIntent = new Intent((ParentActivity)getActivity(), YourOtherActivity.class);
(ParentActivity)getActivity().startActivity(myIntent);
or this way it is a small example. it works for me.
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)
在故障单类中使用此代码 -
TicketFragment tkt = new TicketFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
答案 2 :(得分:0)
TicketFragment fragmentB = (TicketFragment)getFragmentManager().findFragmentById(R.id.fragmentBId);
fragmentB.performSomeTask();
这是从活动中调用片段的最简单方法。
Fragment fragment = null;
fragment = new HomeFragment();
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.layout_lower_right,fragment).commit();
}
public class HomeFragment extends Fragment
{
}