Mark Fidell
Mark Fidell

Reputation: 1123

Display dialog from android.app.application

I have an update routine (connects to server and looks for a newer version of itself) that runs onCreate on my first activity. If there is an update then a dialog is presented to the user, if update isn't mandatory, asking if they want to download the update.

The problem is that I don't want to run this in the activity as it gets called each time the activity is reloaded, like when device is rotated.

I could create a singleton which knows if the check has already been performed or something similar but would prefer to make the check outside of the activity as it just doesn't seem like the right place to do it.

The problem I have is if I put it in the android.app.application then I have no activity to create the dialog. I could create a loader activity just for this and then fire the main activity after but this also doesn't seem ideal.

What would be a good way to handle this scenario?

Upvotes: 1

Views: 250

Answers (2)

Mehul Joisar
Mehul Joisar

Reputation: 15358

crdnilfan has given good answer.and if you want to do it in your MainActivity,then you can maintain Activity Lifecycle in proper manner like described here and if you want to restrict to call onCreate() whenever you rotate your device,you can specify configChanges,

        <activity
        android:name="com.example.temp.MainActivity"
        android:configChanges="orientation"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

I hope it will be helpful !!

Upvotes: 0

bclymer
bclymer

Reputation: 6771

You can't launch an activity from the Application context, sadly it's just not possible. What you need to do is create an activity with

android:theme="@android:style/Theme.Dialog"

within the activity tag in your AndroidManifest.xml. In this activity you should define a fairly simple layout to make it look as much like a dialog as possible (or not if you want it to look like something else).

Another option is to have your first activity setup a BroadcastReceiver and then have the Application context

sendBroadcast(new Intent("MyUniqueID"));

Within the onReceive method of the BroadcastReceiver display your dialog with the Activity context.

Upvotes: 2

Related Questions