Office Hours — Today, March 7

Yesterday, March 6

Mark M.
has entered the room
Mark M.
turned on guest access
Mar 7
7:30 PM
Julien M.
has entered the room
Mark M.
hello, Julien!
how can I help you today?
Julien M.
Hi mark, first of all your book is fantastic, second year i buy it.
Mark M.
thanks!
Julien M.
I need some help, i have a notification, when a user click on it, i want to display an alert dialog on top of my activity. But only when we come from the notification.
The problem i have is screen rotation
when i rotate the screen, the same intent is lunch and the dialog is shown
I don't really want that. I just want my dialog on a click on the notification.
Mark M.
where are you displaying the dialog? onCreate()?
7:35 PM
Julien M.
yes
Mark M.
the Bundle passed into onCreate() will be null when your activity is initially created and non-null after a configuration change (such as a screen rotation)
hence, only display the dialog when the Bundle is null
Julien M.
ok i will try that
The same problem occurred when i launch an activity from history (long press on home button) (activity previously launched by the click on the notification)
Mark M.
that will not be helped by the Bundle trick
is your current code always displaying the dialog? or are you examining something about the Intent to decide whether to display the dialog?
Julien M.
i examine the action of the dialog
which i set on the intent of the pendingintent
of the notification
7:40 PM
Mark M.
do you mean the action of the Intent?
Julien M.
yeah the action of the intent sorry
Mark M.
the problem is that the Intent action is part of the history and therefore will be used by the recent tasks list
Julien M.
I partially resolved this issue checking the flags
to know if it comes from history
Mark M.
another possibility is to simply not have it be part of the history, via android:noHistory="false" on the <activity>
yet another possibility is to cheat a bit and use a boolean value in a static data member
call it doWeNeedTheDialog
set it to false when you initialize it
Julien M.
interesting
Mark M.
have your PendingIntent actually route to a BroadcastReceiver, which sets it to true before starting the activity
your activity shows the dialog if true, plus sets it to false
since your process isn't going to be terminated in that short window, you should be safe
Julien M.
good idea
Mark M.
and the data is totally transient, not part of history or anythihng
er, anything
it would also solve your configuration change issue
but, it's a bit of a hack
Julien M.
Actually the same problem occurred when the screen goes of and on.....
7:45 PM
Mark M.
that too would be handled by this hack
Julien M.
Yes
So i guess for my scenario
i need to use this hack
to be sure that the dialog shows up only on a click on the notification
and not on all the other time watever happens
Mark M.
it's certainly one option
7:50 PM
Julien M.
Do you think there is an 'android official way' of doing that ?
take care of all scenario ?
Mark M.
if I knew of one, I would have suggested it instead of this hack :-)
Julien M.
yeah dumb question ^^
Mark M.
is there anything else that I can help you with?
7:55 PM
Julien M.
Yes
I have an activity with a FragmentPagerAdapter and a ViewPager, in the getItem of the adapter i instanciate my Fragment and keep a reference in a field of my activity.
Mark M.
is this field holding *all* the fragments? presumably you have more than one page in the pager
Julien M.
No just one specific field for one specific fragment i need to interact with
Mark M.
oh, OK
Julien M.
Could i assume in onResume of my activity
that my fragment will be not null
?
8:00 PM
Mark M.
I do not think that this is a safe assumption, though it may work for the current version of ViewPager
Julien M.
ok
Mark M.
I am assuming that you are creating the adapter in onCreate() of the activity
Julien M.
yes
we got the activity state in front of fragment state
and the resumed state of the activity
seems to imply that the fragment is instanciated
But actually i have ran some test
and when i call programmatically finish() on my activity, and restart it
my field is null
Mark M.
that diagram assumes that the fragment is being instantiated in onCreate() of the activity
you are not doing that, based on your description
you are having the FragmentPagerAdapter create this fragment
which may or may not happen during onCreate() of the activity
8:05 PM
Julien M.
why that ?
in onCreate i instanciate my adapter
Mark M.
because you have no real control over when getItem() is called
Julien M.
yes
Mark M.
hence, getItem() *might* be called during onCreate(), or it might not
Julien M.
this is the problem i thought about..
Mark M.
if you *really* need this one special fragment created in onCreate(), do so yourself, and have the FragmentPagerAdapter use it
Julien M.
yes you right
thanks a lot, it really helps !
Mark M.
you are very welcome
8:10 PM
Julien M.
good evening, time to go to bed for me. Thanks again ! BYE
Mark M.
bye!
Julien M.
has left the room
8:30 PM
Mark M.
turned off guest access

Yesterday, March 6

 

Office Hours

People in this transcript

  • Julien Maffre
  • Mark Murphy