How to load popup window on android application st

2019-08-06 12:59发布

问题:

I am new to android development and i want to ask how to load popup window on android application startup? onCreate()?

I have seen many examples but no one covered my needs. Is there a way to load a popup window on application start?

Thank you

回答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"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
  <TextView android:text="hello, world" 
            android:id="@+id/TextView01"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"/>
   <Button android: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

@Override
protected void onStart()
{
    super.onStart();

    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);
    dialog.setTitle("Dialog box");

    Button button = (Button) dialog.findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {  
        @Override  
        public void onClick(View view) {  
            dialog.dismiss();            
        }  
    });

    dialog.show();
}


回答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:

  1. Create an XML layout and place it in res/layout/PopupActivity.xml
  2. Create a new activity, PopupActivity.java.
  3. Add the new activity to your Android manifest file.
  4. 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