2

My app contains two activity, one widget and a broadcast receiver.

First activity is default one which is called by home launcher. Second activity is a reminder dialog activity which plays a MP3 and has a dismiss button. It will appear on home screen.

My widget is a custom clock which setup an alarm to call a broadcast receiver every minutes which is responsible to update the clock. There are some reminders at specific time which broadcast receiver calls the reminder activity.

My app now is fully working, but there is a small problem: When app is open in the background (It's not on screen), when broadcast receiver start the reminder activity, the default activity comes first and reminder activity is shown over that. But when I close the app, reminder activity appears on home screen and there is no problem.

This is the code I use to startActivity from receiver:

context.startActivity(new Intent(context, ActivityReminder.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP));

And this is android manifest:

    <activity android:name=".MyActivity"
        android:theme="@android:style/Theme.Black.NoTitleBar"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity android:name=".ActivityReminder" android:label="@string/reminder" android:screenOrientation="portrait" android:theme="@style/Theme.CustomDialog"  />

    <receiver android:name=".widget"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/widget" />
    </receiver>

    <receiver android:name=".UpdaterReceiver" />

For now I use this workaround in the default activity to solve the problem, it work just like closing the app:

@Override
protected void onPause()
{
    super.onPause();
    finish();
}

I found something more annoying. If the main activity is not closed, every call from broadcast receiver opens the activity again even it's open already. Although if the main activity is closed, subsequent call won't do anything.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Ali
  • 21,572
  • 15
  • 83
  • 95
  • Try to get the intent passed to onCreate on the MyActivity. Maybe you could find why it is called or isolate a parameter that, if present, could be the sign to finish the activity. Good question though, this behaviour is really strange. – Snicolas Dec 29 '12 at 14:16
  • @Snicolas I added onResume and onNewIntent and insert a log line in both of them. But nothing. It's like that the activity doesn't event get called. It just pop up on the screen. – Ali Dec 29 '12 at 14:27
  • I'm guessing that since your broadcast receiver is not part of the activity stack, the clear top and single top flags probably aren't working like you expect them to. Try adding android:launchMode="singleTop" to MyActivity in the manifest and report back. – logray Dec 29 '12 at 16:16
  • @logray Thanks, but didn't work. – Ali Dec 29 '12 at 20:35
  • In addition to the above recommendation, have you tried the intent without clear_top, I've seen some issues when using that with single_top. – logray Dec 29 '12 at 20:53
  • @logray I tested that before. Didn't work. – Ali Dec 29 '12 at 23:33

1 Answers1

3

Your problem is taskAffinity. When you launch ActivityReminder, Android tries to find an appropriate task to launch this into. If your app is not running, it won't find an appropriate task, so it will start a new one and the Activity will show up in a task by itself. However, if your app is already running in the background, Android will bring the existing task to the foreground and launch ActivityReminder into that task.

To solve the problem, add the following to the <activity> definition for ActivityReminder:

android:taskAffinity=""

This tells Android that it shouldn't look for a "matching" task to launch ActivityReminder into.

David Wasser
  • 93,459
  • 16
  • 209
  • 274