Reputation: 93
I am using the AlarmManager
to pass a PendingIntent
to my BroadcastReceiver
after, say, 5 minutes. Now, I want to show a DialogFragment
to the user, on top of whatever app the user might be using when the alarm goes off. For example, if the user is using Chrome when the alarm goes off, my DialogFragment
should popup ABOVE the user's Chrome window.
What I am ending up with instead, is the DialogFragment
being shown with a blank activity of my app as the background (as in the following pic)
https://i.sstatic.net/Vz9IZ.png
This is the code I am using in my BroadcastReceiver
, to launch an FragmentActivity
first :
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent hardReminderIntent = new Intent(context, AlarmHardActivity.class);
hardReminderIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(hardReminderIntent);
}
}
Next, from this activity, I am popping up the DialogFragment
:
public class AlarmHardActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
AlarmHardDialog editNameDialog = new AlarmHardDialog();
editNameDialog.show(fm, "fragment_dialog");
//setContentView(R.layout.activity_alarm_hard);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.activity_alarm_hard, menu);
return true;
}
}
My questions :
I could not find a way to call getSupportFragmentManager
directly from the onReceive
in my BroadcastReceiver
, and thus assumed that the only way to obtain a dialog, would be to first call a 'dummy' activity, that creates the dialog. Is there a better way to do this?
Irrespective of whether or not my approach was correct, I expected that since there is no call to setContentView(..)
in AlarmHardActivity, there would be no UI rendered for it. Is my understanding wrong? I also tried calling setContentView(..)
and then marking the layout to have Theme.NoDisplay
and android:alpha="0"
, but to no avail.
Any help will be much appreciated. Thanks!
Upvotes: 3
Views: 8720
Reputation: 1390
To your questions:
Fragments can be attached only to Activity
(ActivityFragment
). So yes, you need dummy Activity
for this.
Use the invisible theme @android:style/Theme.Translucent.NoTitleBar
, calling setContentView isn't necessary. But this theme isn't Holo compatible (in dialog you would have old Android look). I solved this with custom theme (I use this from ICS
, but it should work from HONEYCOMB
):
<style name="InvisibleTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<!-- Note that we use the base animation style here (that is no animations) because we really have no idea how this kind of activity will be used. -->
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
Upvotes: 3
Reputation: 2960
Instead of showing dialog on the activity, better alternative will be making activity look like dialog by using dialog theme and setting margin of all sides to desirable dp, so that activity get shrieked to dialog box size.
For few examples look at these posts: Android Activity as a dialog
Android: how to create a transparent dialog-themed activity
How to set a dialog themed activity width to screen width?
Upvotes: 5
Reputation: 4119
It is impossible to show this dialog without Activity. You can try to run your app like you have on your screenshoot, but with transparent background and hidden ActionBar. But i have never tried to do that so I'm not sure if it will work.
Upvotes: 0