Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

@Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {             Fragment f = LoginFragment.newInstance();              getSupportFragmentManager()                     .beginTransaction()                     .add(android.R.id.content, f, "loginfragment")                     .attach(f)                     .commit();         } } 

LoginFragment shows a view like this:

@Override     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);         b.setOnClickListener(this);          return v;     } 

when clicking login I show a list view like this:

BeginActivity top = (BeginActivity) getActivity(); Fragment f = OfferListFragment.newInstance();         top.getSupportFragmentManager()                 .beginTransaction()                 .add(android.R.id.content, f, "offerList")                 .addToBackStack(f.getClass().getSimpleName())                 .commit(); 

and finally, OfferListFragment displays its view like this:

@Override     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).

Thanks

like image 844
Darren Avatar asked May 15 '13 21:05

Darren


People also ask

What are fragments and activity?

Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Activity is not dependent on fragment. Fragment is dependent on activity. It can't exist independently.

Can a fragment contain an activity?

A fragment represents a modular portion of the user interface within an activity. A fragment has its own lifecycle, receives its own input events, and you can add or remove fragments while the containing activity is running. This document describes how to create a fragment and include it in an activity.

How many fragments are in activity?

The application can embed two fragments in Activity A, when running on a tablet-sized device.

Can a fragment exist without an activity?

Android app must have an Activity or FragmentActivity that handles the fragment. Fragment can't be initiated without Activity or FragmentActivity.


1 Answers

IMHO I do not agree with this answer.

You may want to replace your fragment or you may want to add it.

For example let's say that you are in a fragment that is a list retrieved by a network request, if you replace the fragment with let's say the detailFragment and you add it to backStack.

When you go back your fragment will redo the network query, of course you can cache it but why? It is a back to a previous state, so with add the last fragment will be in exactly the same status with no code whatsoever.

Fragments are transparent backgrounded by default because they can be used to paint only a small portion of the screen but if your fragment is match_parent then just set its background to a color and keep using add on the fragmentTransaction.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:background="@android:color/white" android:layout_width="match_parent" android:layout_height="wrap_content" > 

That will be your root element on the fragment layout XML, could be linear, etc etc and the transaction code is:

YourFragment detail = YourFragment.newInstance(Id); ft.add(R.id.contentHolder, detail); ft.addToBackStack(TAG); ft.commit(); 

Hope this helps someone that wants to know how to see a solid background without changing the add to replace which is usually not the best case.

Regards

like image 79
Goofyahead Avatar answered Sep 27 '22 19:09

Goofyahead