在一个片段,如果我按后退按钮进入SettingsActivity.class
问题描述:
我有一个activity_settings.xml
文件就像该文件下面我有3层文本的意见,如果我点击的话,会带我去一个片段在一个片段,如果我按后退按钮进入SettingsActivity.class
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.crowderia.chat.SettingsActivity"
android:background="@color/black">
<TextView
android:id="@+id/tv_added_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Added Me" />
<TextView
android:id="@+id/tv_add_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Friends" />
<TextView
android:id="@+id/tv_my_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Friends" />
<FrameLayout
android:id="@+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
我SettingsActivity.class像下面
每个文本视图onclick事件
tv_added_me.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setFragment(new AddedMeFragment());
}
});
组片段方法
public void setFragment(Fragment f) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragmentTransaction.replace(R.id.settings_fragment, f);
fragmentTransaction.commit();
}
如果我点击每个文本视图它会带我去每个片段,但之后我点击回到它不会去设置活动我怎样才能使这项工作
请帮我进出口新的这东西
答
您应该从点击返回按钮中删除FragmentManager
的所有片段。
FragmentManager fm = getSupportFragmentManager();
for (Fragment f : fm.getFragments()) {
fm.beginTransaction().remove(f).commit();
}
我应该在哪里放这个,你可以在@override onBackPressed()方法中解释一下 – HemalHerath
。 –
如果您愿意在设备后退按钮上执行操作,则应写入'onBackPressed()'。如果您使用带后退箭头的工具栏,则应该在后退箭头的点击中写入。 – Wizard