mardi 14 juin 2016

Start Fragment From Back Button Within An Activity

I would like to override the back button in an activity so that I can start a fragment, normally within activities I use an intent but as I understand I cannot use an intent to call fragments.

The reason I want to use the back button to start the intent is so it can reload new information from the database, otherwise if I don't override the back button it will just load the previous fragment with old (cached) data.

My code below which does not work:

@Override
public void onBackPressed(){

    handler.removeCallbacksAndMessages(null);

    new leftRoom().execute();

    Intent intent = new Intent(ActivityChatRoom.this,Fragment_01_Rooms.class);
    startActivity(intent);
}

Update:

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>

Aucun commentaire:

Enregistrer un commentaire