When staring my app a list (list_main) is displayed in the master fragment. There is also a 2nd list fragment that is supposed to open when I clicking the 1st list item from my main list fragment, however after clicking the 1st list item, the main list fragment DOES disappear but the 2nd list does NOT appear hence the master fragment remains blank. Also when I click the back button, the main list does NOT reappear. Does anyone know why these issues are occurring and how they can be resolved?
Main Activity XML
<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:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="@+id/detail_container"/>
</LinearLayout>
Main List Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentmainlist">
<ListView
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"/>
</LinearLayout>
Main List fragment class
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentMainList extends android.support.v4.app.Fragment {
ListView list_main;
String[] listContent = {
"Item 1",
"Item 2",
"Item 3"
};
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main_list, container, false);
list_main = (ListView)v.findViewById(R.id.list_main);
list_main = (ListView)findViewById(R.id.list_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listContent);
list_main.setAdapter(adapter);
list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (mTwoPane) {
for (int j = 0; j < adapterView.getChildCount(); j++)
adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
// change the background color of the selected list view item
view.setBackgroundColor(Color.parseColor("#00BCD4"));
}
if (position == 0) {
if (mTwoPane) {
FragmentItem1 newFragment = new FragmentItem1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
list_main.setVisibility(View.GONE);
transaction.replace(R.id.master_container, newFragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack(null);
transaction.commit();
} else {
}
}
if (position == 1) {
}
if (position == 2) {
}
}
});
return v;
}
}
Main Activity class
public class MainActivity extends ActionBarActivity {
ListView list_main;
String[] listContent = {
"Station Chooser",
"Item 2",
"Item 3"
};
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_main = (ListView)findViewById(R.id.list_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listContent);
list_main.setAdapter(adapter);
list_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0) {
if (mTwoPane) {
FragmentStationChooser newFragment = new FragmentStationChooser();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
getSupportFragmentManager().executePendingTransactions();
} else {
}
}
if (position == 1) {
}
if (position == 2) {
}
}
});
if (findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}
}
}
Station Chooser fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_linechooser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"/>
</LinearLayout>
Station Chooser fragment class
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentStationChooser extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_station_chooser, container, false);
}
}
Station Chooser Activity class
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class StationChooserActivity extends ActionBarActivity {
ListView list_linechooser;
String[] listContent = {
"Line 1",
"Line 2",
"Line 3"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_station_chooser);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
list_linechooser = (ListView)findViewById(R.id.list_linechooser);
list_linechooser.setAdapter(adapter);
list_linechooser.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
}
});
}
@Override
public void onBackPressed() {
if (mTwoPane) {
FragmentMainList newFragment = new FragmentMainList();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
list_main.setVisibility(View.VISIBLE);
transaction.replace(R.id.master_container, newFragment).commit();
transaction.addToBackStack(null);
}
}
}
The main problem why the fragment is not showing upon poping the backstack is that you are trying to inflate that fragment in another fragments id which is this part of the code:
transaction.replace(R.id.master_container, newFragment);
You don't need a whole new activity just to host another fragment which you did in your FragmentStationChooser
.
Now what you need to do is to have a container to host your fragment in your activity main so instead of having a fragment tag replace it with a container such as relative layout:
replace this:
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"/>
to
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/master_container"/>
After you do that, In your main activity remove all the listview code and transfer it in to your FragmentMainList
where you use the view when you inflate it:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_main_list, container, false);
list_main = (ListView)v.findViewById(R.id.list_main);
.//the logic from your activity main transfered here in the FragmentMainList
.
.
return v;
}
Lastly do it the same in your FragmentStationChooser
and don't use the activity StationChooserActivity
just remove it from your project
EDIT:
remove the list_main.setVisibility(View.GONE);
due to the fact the it will be replace by a fragment when it is inflated
EDIT 2:
in the onCreate of your main activity add this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentMainList newFragment = new FragmentMainList();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
}