Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

starting a new activity in onCreate works only with delay

I have a simple activity that just shows two buttons, and I would like to load another activity as soon as that one is finished loading.

@Override
public void onCreate(Bundle savedInstanceState) {
    dbg("starting on create");
    super.onCreate(savedInstanceState);

    dbg("stting content view");
    setContentView(R.layout.main);

    createDrPopup();

}

private void createDrPopup(){
    dbg( "created new activity");
    startActivityForResult(new Intent(this, DrPopup.class), 
                           DR_POPUP_ACTIVITY_ID);
}

I don't get any errors with this code, but the new activity doesn't load properly. This way I only get a blanc screen.

But if I call the new activity with a delay, then everything works fine.

@Override
public void onCreate(Bundle savedInstanceState) {
    dbg("starting on create");
    super.onCreate(savedInstanceState);

    dbg("stting content view");
    setContentView(R.layout.main);

    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                sleep(500);
            } catch(InterruptedException e) {
            } finally {
                createDrPopup();
            }
        }
    };
    splashTread.start();
}

private void createDrPopup(){
    dbg( "created new activity");
    startActivityForResult(new Intent(this, DrPopup.class), 
                           DR_POPUP_ACTIVITY_ID);
}

my question is this:

Is there any better way to wait for an activity to finish loading. I can't even be sure that 500ms will be enough each time.

Thank you for any suggestions.

requested code for dr_popup (this is as simple as it gets)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    dbg("Created new activity");

    setContentView(R.layout.dr_webview);
    dbg("web view created");

}

and both layouts

main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Spinner android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:id="@+id/spinner1"></Spinner>
</LinearLayout>

popup

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dr_popup_webview_layout" android:layout_width="fill_parent" android:layout_height="fill_parent"  android:orientation="vertical">
    <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dr_popup_webview" android:layout_width="fill_parent"  android:layout_height="100dip"/>
    <LinearLayout  android:id="@+id/frameLayout1" android:layout_height="100dip" android:layout_width="fill_parent" android:orientation="horizontal">
        <EditText android:id="@+id/dr_submit_text"          android:layout_height="wrap_content"            android:layout_width="wrap_content"         android:layout_weight="4">
            <requestFocus></requestFocus>
        </EditText>
        <Button             android:text="Button"           android:id="@+id/dr_submit_button"          android:layout_height="wrap_content" android:layout_width="wrap_content"    android:layout_weight="1"></Button>
    </LinearLayout>
</LinearLayout>

edit: the problem seems to be caused by the manifest line @android:style/Theme.Dialog

    <activity android:theme="@android:style/Theme.Dialog"
              android:name=".DrPopup"
              android:label="@string/dr_popup">
    </activity>

If I remove that, things seem to work, but I would like a solution that would work even with that line.

and a picture that shows the problem. http://i.imgur.com/ZOcyw.png

like image 985
zidarsk8 Avatar asked Jun 30 '11 16:06

zidarsk8


People also ask

What is onCreate () meant for?

onCreate() You must implement this callback, which fires when the system first creates the activity. On activity creation, the activity enters the Created state.

Is onCreate only called once?

OnCreate is only called once.

Under which circumstances does the onCreate () method in your activity receive a bundle with data in it that is the bundle is not null )?

Under which circumstances does the onCreate() method in your activity receive a Bundle with data in it (that is, the Bundle is not null )? The activity is restarted after the device is rotated.

How many times does onCreate run?

You might want to read through the documentation on the Activity lifecycle. OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.

Why do we override onCreate?

onCreate is "Overridden" because Activity has an existing implementation that your class MainActivity is replacing with it's own implementation. you would say "implemented" if the method is only declared in an interface, but there is no implementation in a super class your are replacing.


1 Answers

I don't see any reason from the provided code why do you have to insert a 500 ms delay… That's just a non-sense to insert that delay. Some clarifications are needed: 1. Did you do heavy processing within the original activity? 2. startActivityForResult is called with purpose (since your code fragments doesn't show any onActivityResult implementation)?

I will try to implement a minimal example and update the post asap…

−− Update

After several attempts to understand and fix this issue with a parallel chat session, I reproduced the problem on a fresh 1.5 emulator. The problem finaly comes from the use of android:theme="@android:style/Theme.Dialog" for the callee activity. The issue is observed on android 1.5 (both device and emulator). Whatever is declared in the second layout, whenever the associated activity is started, nothing is displayed. The display is just blank and a push on the back button initiates a transient display of the activity but go to the first activity (later part is nominal). I could not explain why this happens and I suggest to zidarsk8 to give PopupWindow a try or define a custom style (trial/error approach based on android Theme.Dialog source definition).

like image 100
Renaud Avatar answered Sep 25 '22 04:09

Renaud