jnthnjns
jnthnjns

Reputation: 8925

The Original Fragment won't go to back of stack, it stays visible

I am having some issues with FragmentTransactions. I have sucessfuly called on a fragment from the onClick method but the original mainFragment won't "disappear" or go to back of stack. I have followed the Android Development tutorial on Fragment Transcation. I think the issue is in my Survey.java class or my Details.java class. Any help is appreciated.

Update: (Added Photos below)
Before: http://imm.io/q9vt
After Clicking "Survey Button": http://imm.io/q9wf

See code for all below:

Code from Main.java

package mycompany.nst;

import mycompany.nst.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    Fragment _surveyFrag = new Survey();

    public void focusFragment(Fragment mainFragment) {
        FragmentManager FragMgr = getFragmentManager();
        FragmentTransaction transaction = FragMgr.beginTransaction();
        try {
            transaction.replace(R.id.mainFragment, _surveyFrag);
            transaction.addToBackStack(null);
            transaction.commit();

        } catch(Exception e){};
            transaction = null;
            FragMgr = null;     
    }

    public void survey_onClick(View view) {
        Log.i("onClick", "SURVEY BEGIN");
        focusFragment(_surveyFrag);
    }
}

Code from main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/hsdarker"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/listFragment"
        android:name="mycompany.nst.ListFragment"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        class="mycompany.nst.ListFragment" >
        <!-- Preview: layout=@layout/list -->

    </fragment>

    <fragment
        android:id="@+id/mainFragment"
        android:name="mycompany.nst.Details"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/listFragment"
        class="mycompany.nst.Details" >
        <!-- Preview: layout=@layout/details -->

    </fragment>


</RelativeLayout>

Code from Details.java <-- The original fragment

package mycompany.nst;

import mycompany.nst.R;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

    public class Details extends Fragment {
        /** Called when the activity is first created. */
        @Override
        public View onCreateView(LayoutInflater LIdetails, ViewGroup VGdetails, Bundle SaveInstancedState) {
            Log.i("DetailsFragment", "onCreateView");
            // Inflate the layout for this fragment
            return LIdetails.inflate(R.layout.details, VGdetails, false);
        }  
    }

Code from Details.java <-- The new fragment

package mycompany.nst;

import mycompany.nst.R;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

    public class Survey extends Fragment {
        /** Called when the activity is first created. */ 
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Log.i("SurveyFragment", "onCreateView");
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.survey, container, false);
        }
    }

Upvotes: 0

Views: 421

Answers (1)

Alexander Lucas
Alexander Lucas

Reputation: 22371

You can't replace or remove a fragment that's defined in XML- it becomes a non-removable part of the layout. However, if you define the fragments in code, you can replace and remove them at will.

Upvotes: 3

Related Questions