简体   繁体   中英

App becomes background of the device

I found a strange bug, when I press the home button from my android app, the app becomes the background of my device (Note II) for less than a second and then disappears.

I think it is related to:

android:theme="@android:style/Theme.Translucent.NoTitleBar"

that I have put for my main activity. How can I fix this bug but keeping the effect of not displaying anything until the app has loaded (like Quora)?

EDIT: I've just tested, and when replacing the line with android:theme="@android:style/Theme.NoTitleBar" , the bug is not here anymore; but now when the app launches, there is a black screen flashing. (And that was why I had put translucent )

You either have a transparent background or not. Means the Apps background is transparent when it starts AND when it stops.

As you want your background transparent at start but then not anymore, you can create a (kind of) Splash screen that is just a transparent, fill_parent rectangle and have a normal theme.

One remark about this. Even when you see the home screen during startup of the App, any clicks and touches will already be routed to your app. A user might be confused that he/she still sees the home screen but cannot use it. Furthermore he/she might be confused because they started your app but it seems like nothing happens.

EDIT: The idea is, to create a seperate layout, with just a transparent background and, when the app starts, use this layout before the actual app with its layout starts. This is like a Splash screen but transparent.

Here's some code to illustrate.

First the layout of the splash:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#00000000" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Starting..."
        android:background="#FF000000"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Then the splash activity:

public class Splash extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        startActivity(new Intent(this, YOUR_ACTUAL_ACTIVITY.class));
        finish();
    }
}

For testing you might want to add some waiting before you start the actual application activity to see the splash for a moment.

I have solved a similar problem -- I dislike the initial flash of a white background when my app loads with a user-selected dark theme. Here's the approach I use:

1) In the Manifest's application section, set the theme to use at startup. I use an opaque one, but here it is with a transparent one:

<manifest ... >
    ...
    <application
        ...
        android:theme="@android:style/Theme.Translucent.NoTitleBar" >

2) In my activities -- all of my activities -- I update the theme like so:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_NoTitleBar); //or whatever theme you want
    setContentView(R.layout.activity_main);

(You may be able to set the activity theme in your layout files instead. I do it dynamically, because it's actually a user option in my case. If doing it dynamically, it must be before the setContentView call.)

Note that I agree with jboi that you shouldn't really have a completely transparent app while it is loading -- The user wants some immediate feedback that they have successfully activated your app icon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM