7

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. https://i.stack.imgur.com/6hhge.png

zidarsk8
  • 3,088
  • 4
  • 23
  • 30
  • Hi can you also provide the code for DrPopup. – Deva Jun 30 '11 at 16:26
  • 1
    Well, I don't know for sure, but perhaps the ActivityManager cannot handle starting a new Activity when another Activity is still in the onCreate() lifecycle method. The timer allows for enough time for onCreate() to complete... – nicholas.hauschild Jun 30 '11 at 16:35
  • I've added the code and the xml files as well, and I should add I'm working on android 1.5 – zidarsk8 Jun 30 '11 at 16:48

1 Answers1

5

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

Renaud
  • 8,783
  • 4
  • 32
  • 40
  • I agreee it's total nonsense, that's what I'm asking here, cause I have no idea why this would work only with the delay. But that seems to be the case. – zidarsk8 Jun 30 '11 at 16:46
  • I just tried from onStart, but the result is the same, starting without the delay makes the new activity without any buttons or anything. – zidarsk8 Jun 30 '11 at 16:51
  • It works here without delay (with a plain startActivity) except that the WebView appears after other layout elements. – Renaud Jun 30 '11 at 16:54
  • I have tried id Again with the code I posted here, and it works, But there is one thing I forgot to post The **manifest** File. Thanks to you I now know the problems is with the line `android:theme="@android:style/Theme.Dialog"` it if you put that into the second activity, things probably wont work – zidarsk8 Jun 30 '11 at 17:13
  • 1
    OK, from your screenshot, I've realized that you mean popup by using popup ;) By the way, why not use a PopupWindow? – Renaud Jun 30 '11 at 17:20
  • This is my first app in android, and it's just how i started working on it. And the popup will have quite a few things to do, so I thought that starting a new custom Activity would be better. – zidarsk8 Jun 30 '11 at 17:34
  • Reported to work here using '' – Renaud Jun 30 '11 at 17:38
  • @zidarsk8 let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1022/discussion-between-renaud-and-zidarsk8) – Renaud Jun 30 '11 at 17:39