我想重寫活動中的後退按鈕,以便我可以啓動片段,通常在活動中使用意圖,但據我瞭解,我無法使用意圖調用片段。從活動中的後退按鈕開始片段
我想使用後退按鈕來啓動意圖的原因是它可以從數據庫重新加載新信息,否則如果我不覆蓋後退按鈕,它將只加載舊的(緩存)數據。
我下面的代碼不工作:
@Override
public void onBackPressed(){
handler.removeCallbacksAndMessages(null);
new leftRoom().execute();
Intent intent = new Intent(ActivityChatRoom.this,Fragment_01_Rooms.class);
startActivity(intent);
}
更新:
fragment_01_rooms.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- android:background="@color/gray_header"-->
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fadeScrollbars="false" >
</ListView>
</LinearLayout>
更新(activity_main.xml中):
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/viewpager"
tools:context=".FragmentMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
我得到「容器無法解析或不是字段」我試圖添加android:id =「@ + id /容器」到fragment_01_rooms.xml,沒有工作。 – user3560827
它不會。正如我在我的回答中所提到的,您需要爲要在其中激活此「Fragment」的「Activity」添加一個「FrameLayout」。這個'FrameLayout'應該有id「container」。如果您發佈了「活動」的XML,我將能夠更好地爲您提供幫助。 –
我更新了fragment_01_rooms.xml的內容 – user3560827