Android: startActivity() starting a "chain" of activities?
Yes, I know. It sounds weird, but I can't think of any other way to explain it.
I start Activity MainActivity. It calls startActivity(B). B calls startActivity(C). In C, I have a button with an onClickListener that looks like this:
startActivity(new Intent(getApplicationContext(), MainActivity.class));
When I hit the button, I get to activity B, not Main!
LogCat shows this:
Activitymanager Starting activity: ... MainActivity ActivityManager Starting activity: ... BSo, it looks like my MainActivity (A) gets started and then something-or-someone-mysterious starts activity B immediately.
Ideas? I'm at a loss...
Thanks!
LlappallEDIT: A couple of notes to clarify:
1) I'm not overriding any lifecycle methods in MainActivity. I just have onCreate. 2) MainActivity's layout shows an image that covers the whole screen. I capture where the user touches in the image's OnTouchListener and, depending on that, I call different intents. The code looks like this:if (touchX > 0 && touchX < 0.5 && touchY > 0.25
&& touchY < 0.46) {
Intent i = new Intent(conte开发者_运维知识库xt, ShowListsActivity.class);
i.putExtra("option", 0);
startActivity(i);
} else if // if the location of the click is different I call another .class
In C, I have a button:
startActivity(new Intent(getApplicationContext(), MainActivity.class));
You should use this
instead of getApplicationContext()
.
I start Activity MainActivity. It calls startActivity(B).
Ok, so MainActivity
(A
) calls B
as soon as it starts?
Therefore it seems completely reasonable that B
starts when you call the MainActivity
intent.
Presumably you're calling "start B" in the onResume
method of MainActivity
— that means, every time you return to A
it will immediately start B
again.
Instead, you should probably call "start B" only from onCreate
.
Though as codelark says above, it would be helpful to detail the lifecycle methods you are employing.
精彩评论