How To Load Popup Window On Android Application Startup? Oncreate()?
Solution 1:
The best place to do this is in the onStart
method of your Activity. Essentially, we need to:
- Get a new Dialog, and specify the XML layout you want.
- Fill in any other options, including code to run when the user clicks on the buttons on the dialog (in this case, simply closing the dialog).
- Show the dialog.
Here's a simple example, but there are lots of options avaiable. For more information, see http://developer.android.com/reference/android/app/Dialog.html.
res/layout/dialog.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:text="hello, world"android:id="@+id/TextView01"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/Button01"android:layout_below="@id/TextView01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="OK" /></RelativeLayout>
In your activity
@OverrideprotectedvoidonStart()
{
super.onStart();
finalDialogdialog=newDialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Dialog box");
Buttonbutton= (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
Solution 2:
You want to create a new activity and launch it via an Intent. Basically, if you want it to 'feel' like a popup, you just create a layout that does not take 100% of the screen's width/height.
Here is how you can do that:
- Create an XML layout and place it in res/layout/PopupActivity.xml
- Create a new activity, PopupActivity.java.
- Add the new activity to your Android manifest file.
- Launch the activity via an Intent from the onResume method of your main activity:
startActivity(new Intent(this, PopupActivity.class));
If you want to receive notification when the user finished with your popup activity, you can launch it using startActivityForResult() instead of just startActivity(). This allows you receive a callback when the user has finished with the activity.
I recommend using onResume() to launch because it is always called when the user returns to your activity, either the first time, or subsequent times.
You will need persist a value representing the state of your application in order to decide when/if you will display your PopupActivity. Android will periodically kill off your application as needed, therefore, if you do not implement some sort of persistence, then your PopupActivity will always appear when the user comes to the application. I would start be implementing onSaveInstanceState() and onRestoreInstanceState() in your main activity.
A simple way to know whether to show the PopupActivity or not, is to just have a variable, such as popupShown
set it false by default, and true after the popup has been show. Before launching the popup, check if popupShown
is true. Saving the state of the application, as I mentioned above, will ensure that you only show the popup when needed.
It's very important to understand how Android manages the lifecycle of activities, otherwise you will be wasting a lot of time wondering why your application is behaving oddly. My first Android application was very much unusable because I just dove in and didn't fully understand the lifecycle.
You should read through this if you aren't completely sure on the lifecycle bit: http://developer.android.com/reference/android/app/Activity.html
Post a Comment for "How To Load Popup Window On Android Application Startup? Oncreate()?"