Office Hours — Today, October 20

Tuesday, October 18

Oct 20
7:25 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:30 PM
yuku
has entered the room
Mark M.
howdy, yuku!
yuku
Hi Mark
Mark M.
how can I help you today?
yuku
long time didn't see the chat room
I've a question..
I'm using c2dm to receive notifications from server, and when I receive it, I put a notification status bar
7:35 PM
yuku
View paste
The pending Intent I use for the notification, contains the Intent that opens the "root" activity, with 		res.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		res.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
and that Intent also contains an extra to let the root activity knows which activity to open on top of it
hope that makes sense?
Mark M.
so far, yes
yuku
ok, so, let's say the root activity has opened screen A due to the user tapping on the notification.
*i mean activity A
then, the user navigates to other activities.
the problem I'm seeing is, when the user switches to another application, and back to my application, my application shows activity A instead of the last activity the user was using.
the behavior is as if the user clicked on the notification *again*.
Mark M.
how does the "user navigate to other activities"?
are you using FLAG_ACTIVITY_NEW_TASK on those too?
7:40 PM
yuku
I mean the user dismisses activity A or opens other activities. No, I don't use any flags to open the other activities.
Mark M.
so, you are starting Root (via the PendingIntent), Root starts A (with no flags), A starts B, B starts C, ... (all with no flags)
then user presses HOME
then user long-presses HOME to return to your task
and they wind up back at A and not at C (or wherever)
right?
yuku
yes!exactly
Mark M.
does this happen all the time?
for example, if the user presses HOME and *immediately* tries returning, does it happen?
or will it only happen if the user is gone for a bit?
yuku
Wait, let me try.
7:45 PM
yuku
It happens immediately
Mark M.
hmmmm
I haven't poked in all the dark and dusty corners of Android's tasks system
my thought was that perhaps this was only occurring if your process was terminated
and so Android is restarting the process and its task based on the Intent that was used to create the task in the first place
however, if it happens immediately, you would think your process would still be around
have you used logging or breakpoints or anything to determine which of your activities are getting control when the user comes back to the task?
yuku
To me, it looks like when the task switcher sends an Intent to activate my app, the Intent that is received by my app is still the one sent by the notification
Mark M.
agreed
yuku
but I need to confirm that.. let me try that for a minute
Mark M.
and I know that the recent tasks list is tied somewhat to that Intent
however, I thought it was only if the process was gone
that being said, as I said, I haven't tried all the permutations of this stuff
which is one of the reasons there's no chapter on it in the books :-)
yuku
Haha, yeah. this tasks thing is super confusing
Hmm well, my root activity gets onNewIntent called
when I switch back to my app using the task switcher
7:50 PM
Mark M.
are you firing off the startActivity() for A in onResume() or something?
yuku
No.
Mark M.
then where in root are you starting up A?
yuku
View paste
in root onCreate, I call this: 			handleNotificationIntent(getIntent());
View paste
in root onNewIntent, I call this 		handleNotificationIntent(intent);
Mark M.
ah, OK
yuku
then A is started inside handleNotificationIntent
whether A is started inside handleNotificationIntent, depends on the small extra on the intent itself.
Mark M.
out of curiosity, what version of Android are you trying this on?
yuku
2.3.something
2.3.3
7:55 PM
yuku
by the way, the root activity has android:launchMode="singleTop"
View paste
and the PendingIntent I use to create the notification is created in this way: 		PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Mark M.
the singleTop may not be helping your cause
yuku
I put PendingIntent.FLAG_UPDATE_CURRENT, because if I don't put that, the root activity doesn't get the extras I put on the intent.
Mark M.
yes, FLAG_UPDATE_CURRENT is typically needed if you are using extras
you might run a test with removing singleTop and see what the behavior is
beyond that, I'm not completely sure why you are seeing this behavior
yuku
OK.. trying.
Mark M.
8:00 PM
yuku
Removing the singleTop doesn't solve it
Thanks for the link. Reading it
Mark M.
if I had to guess, the FLAG_ACTIVITY_NEW_TASK may have something to do with it
I don't know how you can work around matters
but the link suggests that you can at least detect when this condition occurs
yuku
Hey it works!
yeah.
Mark M.
um, what works?
8:05 PM
yuku
It works by removing the FLAG_ACTIVITY_NEW_TASK.
8:05 PM
Mark M.
oh, right, you're starting this from a Notification
I was thinking you were starting it straight from the C2DM message, in which case you'd have no choice but to have that flag
since you need that for starting activities from a BroadcastReceiver
yuku
Oh.. yes, true. I think I messed that up
Mark M.
so that's interesting -- FLAG_ACTIVITY_NEW_TASK not only affects initial launch, but the recent-tasks list as well
yuku
seems so.. if i'm not wrong, what recent-tasks screen does is nothing more than doing startActivity
with some flags applied
Mark M.
pretty much
you can find out the Intents that are used via ActivityManager and getRecentTasks()
I had a blog post about that a while ago, particularly noting that anything you include in a startActivity() Intent, if it winds up in that list, is visible to all applications on the device
yuku
Oh, yah, I read that one
Mark M.
they at least add FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY, and perhaps more
yuku
Thanks for that!
Mark M.
actually, it looks like that's all they add, at least through Gingerbread
yuku
Since no one else is here today.. is another question OK?
Mark M.
oh, go right ahead
8:10 PM
yuku
I'm building custom widgets that consists of multiple child widgets.
So let's say I'm creating UserView that extends from LinearLayout that contains 2 TextViews.
so I begin writing the class
public class UserView extends LinearLayout {
public UserVIew(Context context, AttributeSet attrs) { super(context, attrs);
LayoutInflater.from(getContext()).inflate(R.layout.my_layout_file, this, true);
}}
And my_layout_file.xml contains
<LinearLayout>
<TextView .../>
<TextView .../>
</LinearLayout>
I thought that's the most correct way, but when I see the layout tree, I realized that
the UserView has one child, which is a LinearLayout, and that LinearLayout has 2 TextView s
Mark M.
correct
yuku
What I actually want is the UserView has 2 TextView s
Mark M.
you probably want <merge> as the root instead of <LinearLayout>
yuku
what should I do?
oh.
Mark M.
in this case, ColorMixer is a RelativeLayout, but the same thing holds
8:15 PM
Mark M.
this should be covered in the Custom Views chapter of the Advanced Android book
if I didn't forget
yuku
OK, let me try it for a minute
Oh, yeah, that works!
I heard about <merge> before but I never understand the use of it
thanks, again!
Mark M.
it can also be helpful in conjunction with <include>
layout A uses <include> to add in layout B
but layout B logically is just a bunch of widgets with no specific root
so you use <merge> to satisfy the XML
yuku
understood.
8:20 PM
yuku
no wonder I was seeing too many nested views when using include.
that's all for today, thanks and see you next time :D
Mark M.
ok, have a pleasant day!
8:30 PM
yuku
has left the room
Mark M.
turned off guest access

Tuesday, October 18

 

Office Hours

People in this transcript

  • Mark Murphy
  • yuku