Reputation: 3348
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.sstatic.net/6hhge.png
Upvotes: 7
Views: 5280
Reputation: 8873
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).
Upvotes: 5