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!
|
Mark M. |
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.
|
Julien M. |
The problem i have is screen rotation
|
Julien M. |
when i rotate the screen, the same intent is lunch and the dialog is shown
|
Julien M. |
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()?
|
Mar 7 | 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)
|
Mark M. |
hence, only display the dialog when the Bundle is null
|
Julien M. |
ok i will try that
|
Julien M. |
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
|
Mark M. |
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
|
Julien M. |
which i set on the intent of the pendingintent
|
Julien M. |
of the notification
|
Mar 7 | 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
|
Julien M. |
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>
|
Mark M. |
yet another possibility is to cheat a bit and use a boolean value in a static data member
|
Mark M. |
call it doWeNeedTheDialog
|
Mark M. |
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
|
Mark M. |
your activity shows the dialog if true, plus sets it to false
|
Mark M. |
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
|
Mark M. |
er, anything
|
Mark M. |
it would also solve your configuration change issue
|
Mark M. |
but, it's a bit of a hack
|
Julien M. |
Actually the same problem occurred when the screen goes of and on.....
|
Mar 7 | 7:45 PM |
Mark M. |
that too would be handled by this hack
|
Julien M. |
Yes
|
Julien M. |
So i guess for my scenario
|
Julien M. |
i need to use this hack
|
Julien M. |
to be sure that the dialog shows up only on a click on the notification
|
Julien M. |
and not on all the other time watever happens
|
Mark M. |
it's certainly one option
|
Mar 7 | 7:50 PM |
Julien M. |
Do you think there is an 'android official way' of doing that ?
|
Julien M. |
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?
|
Mar 7 | 7:55 PM |
Julien M. |
Yes
|
Julien M. |
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
|
Julien M. |
that my fragment will be not null
|
Julien M. |
?
|
Mar 7 | 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
|
Julien M. |
in this figure : http://developer.android.com/guide/components/f...
|
Julien M. |
we got the activity state in front of fragment state
|
Julien M. |
and the resumed state of the activity
|
Julien M. |
seems to imply that the fragment is instanciated
|
Julien M. |
But actually i have ran some test
|
Julien M. |
and when i call programmatically finish() on my activity, and restart it
|
Julien M. |
my field is null
|
Mark M. |
that diagram assumes that the fragment is being instantiated in onCreate() of the activity
|
Mark M. |
you are not doing that, based on your description
|
Mark M. |
you are having the FragmentPagerAdapter create this fragment
|
Mark M. |
which may or may not happen during onCreate() of the activity
|
Mar 7 | 8:05 PM |
Julien M. |
why that ?
|
Julien M. |
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
|
Julien M. |
thanks a lot, it really helps !
|
Mark M. |
you are very welcome
|
Mar 7 | 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 |
Mar 7 | 8:30 PM |
Mark M. | turned off guest access |