我想在单击活动的操作栏图标时打开一个片段,并在单击后退按钮(片段的布局)时删除片段(并返回活动)。重要的是我的活动布局中没有<fragment>
。
main activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is main activity layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.settings:
newintent();
default:
break;
}
return true;
}
private void newintent() {
// Do something here to open FragmentOne.class
}
}
答案 0 :(得分:2)
创建一个片段并在您的点击事件方法中添加此代码
private Fragment newFragment=new NewFragment();//global variable
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack();
FragmentTransaction ft =fragmentManager.beginTransaction();
ft.replace(R.id.current_layout, newFragment,"Order");
ft.commit();
您可以点击此链接.. click