我有一个 fragment :
ProductsFragments extends Fragment
和一项 Activity
AdminMenuActivity extends ActionBarActivity
我想从AdminMenuActivity调用ProductsFragments。我使用了2个选项:
1)
FragmentManager fm = getSupportFragmentManager();
for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack();
}
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.frame_layout, android.support.v4.app.Fragment.instantiate(AdminMenuActivity.this, fragments[1]));
tx.commit();
2)
Intent intent1 = new Intent(AdminMenuActivity.this, ProductsActivity.class);
startActivity(intent1);
两者均失败。我不想使用FragmentActivity扩展ProductsFragments,因为它没有给我SupportedActionBar v7
那我怎么称呼
Fragment
? 最佳答案
这是您从 Activity 内部调用 fragment 的方式
Fragment fr = new FirstFragment();
fr.setArguments(args);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
假设您具有fragment_place表示以下内容:
<fragment android:name="com.company.appName.fragments.FirstFragment"
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />