Darren Darren - 12 days ago 8x
Android Question

Fragment is transparent and shows Activity below

My Android application launches into BeginActivity which is a subclass of SherlockFragmentActivity and shows it's first view using:

public void onCreate(Bundle savedInstanceState) {

if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
Fragment f = LoginFragment.newInstance();

.add(android.R.id.content, f, "loginfragment")

LoginFragment shows a view like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.login, container, false);

// Get pointers to text views
usernameField = (EditText) v.findViewById(R.id.usernameLog);
passwordField = (EditText) v.findViewById(R.id.passwordLog);
progressBar = (ProgressBar) v.findViewById(R.id.progressBarLog);
// Set button click listeners for both buttons
Button b = (Button) v.findViewById(R.id.loginButton);

return v;

when clicking login I show a list view like this:

BeginActivity top = (BeginActivity) getActivity();
Fragment f = OfferListFragment.newInstance();
.add(android.R.id.content, f, "offerList")

and finally, OfferListFragment displays its view like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.offers, container, false);

return v;

Now the problem I am having, is that the final OfferListFragment seems to be transparent and I can see the login screen below it. I am using Theme.Sherlock that has a black background. Should I be manually setting the views backgrounds to black also? Or would the black in the theme be customisable by the user on the system? (I'm not an Android user).



Try using FragmentTransaction class to replace the fragments instead of just adding.


Each transaction is a set of changes that you want to perform at the same time. You can set up all the changes you want to perform for a given transaction using methods such as add(), remove(), and replace(). Then, to apply the transaction to the activity, you must call commit().

Before you call commit(), however, you might want to call addToBackStack(), in order to add the transaction to a back stack of fragment transactions. This back stack is managed by the activity and allows the user to return to the previous fragment state, by pressing the Back button.

For example, here's how you can replace one fragment with another, and preserve the previous state in the back stack:


// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);

// Commit the transaction

Reference: Please have a look at Managing Fragments

I hope it will be helpful !!