Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

android - How to finish() an Activity when Home button pressed

For a complicated reason I need to be able to finish() my activities when the user presses the HOME button.

The story here is that I have a homescreen widget that launches a different part of my application that has a completely transparent activity (so the homescreen keeps showing even though my activity is running). If the previous activities were terminated via Home button, they are brought to the foreground and obscure the home screen.

Or as alternative, can I have the new activity somehow force finish() the previous activity?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

what about

android:launchMode="singleTask"

or

android:launchMode="singleInstance"

in your manifest? i think singleTask is the one you want, but im still not crystal clear on what you are doing.

"The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one." singleTask

@Override
void onPause() {
   super.onPause();
   finish();
}

dev docs: Acitvity Lifecycle , Finish


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...