退出/完成应用程序/活动 - android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11081197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exit/Finish an app/activity - android
提问by kumareloaded
I've got 4 activities say Act1
, Act2
, Act3
and Act4
.
A button in Act1 opens Act2, a button in Act2 opens Act3, a button in Act3 opens Act4.
我有4个活动说Act1
,Act2
,Act3
和Act4
。Act1 中的按钮打开 Act2,Act2 中的按钮打开 Act3,Act3 中的按钮打开 Act4。
I want two things to be done:
我想做两件事:
I've a button in Act4 which directs the user to Act1, the prob is when the user clicks back in Act1, i want to close the app instead of opening the Act4..
I've option in menu 'exit' in all activities when the user choose it, i want to close the app instead of going back to previous activity.
我在 Act4 中有一个按钮将用户引导到 Act1,问题是当用户在 Act1 中单击返回时,我想关闭应用程序而不是打开 Act4..
当用户选择它时,我在所有活动的菜单“退出”中都有选项,我想关闭应用程序而不是回到以前的活动。
Tried using finish();
but it didn't meet my requirements.
尝试使用,finish();
但它不符合我的要求。
回答by Praveenkumar
Use below code in your Act4
'th Menu.xml
's exit button -
在Act4
'thMenu.xml
的退出按钮中使用以下代码-
Intent intent = new Intent(Act4.this, Act1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
And, in your first activity's onCreate()
method just put the below code -
而且,在您的第一个活动的onCreate()
方法中,只需输入以下代码 -
if (getIntent().getBooleanExtra("EXIT", false))
{
finish();
}
This will exit your app.
这将退出您的应用程序。
回答by Krishna Suthar
Check out this link:
看看这个链接:
You can use :
您可以使用 :
@Override
public void onBackPressed()
{
moveTaskToBack(true);
}
in all activities to close the app.
在所有活动中关闭应用程序。
回答by Prerak
Intent intent = new Intent(Act4.this, Act1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Clear the flag before you switch back to previous activity. It might help you.
在切换回之前的活动之前清除标志。它可能会帮助你。
回答by Manikumar Gouni
Place this code in your app:
将此代码放在您的应用程序中:
moveTaskToBack(true);
android.os.Process.killProcess(Process.myPid());
System.exit(1);
回答by Dipak Keshariya
finish previous activity when you are go to the next activity means write finish();
after startactivity(intent);
and write below code for start first activity from fourth activity's button click event.
当您转到下一个活动时完成上一个活动意味着finish();
在startactivity(intent);
第四个活动的按钮单击事件之后编写并编写以下代码以启动第一个活动。
Intent in1=new Intent(Act4.this, Act1.class);
startActivity(in1);
finish();
And Write Below Code on Your Exit Button's Click event in all activities.
并在所有活动中的退出按钮的 Click 事件上编写以下代码。
finish();
回答by R Earle Harris
The approach I use is to start all child activities with startActivityForResult
. Then I can putExtra("STATE", some_value) on exiting any child and use the state value to determine what to do up through the parents.
我使用的方法是用startActivityForResult
. 然后我可以 putExtra("STATE", some_value) 退出任何孩子并使用状态值来确定通过父母做什么。
If I want to exit the app from a deep child, "STATE" would be "exit" and each child in the hierarchy would simply get the StringExtra for "exit", do a putExtra("STATE", "exit") and call finish() and then the app would call finish() in the end. You can use this to achieve any desired state.
如果我想从深孩子退出应用程序,“状态”将是“退出”,层次结构中的每个孩子都会简单地获取“退出”的 StringExtra,执行 putExtra("STATE", "exit") 并调用finish() 然后应用程序会在最后调用 finish() 。您可以使用它来实现任何所需的状态。
Using startActivityForResult to pass messages this way avoids having to pass awkward references down into the child activities and simplifies your approach to setting the desired state of the app.
使用 startActivityForResult 以这种方式传递消息可避免将笨拙的引用向下传递到子活动中,并简化您设置应用程序所需状态的方法。
回答by kalan nawarathne
If you have another activity behind ( in the application activity stack),
you could use finish()
to exit the current activity.
如果您有另一个活动(在应用程序活动堆栈中),您可以使用finish()
退出当前活动。
However, the back function too suppose to remove the current activity from your application activity stack.
但是,back 函数也假设从应用程序活动堆栈中删除当前活动。
In my experience, it is very unreliable to rely on the activities in the back stack ( in the application activity stack). Because of that I used to exit each activity when I go deeper.
以我的经验,依赖后端堆栈中的活动(在应用程序活动堆栈中)是非常不可靠的。因此,当我深入时,我曾经退出每个活动。
In your case:
在你的情况下:
Act 1 -> exit -> Act 2 -> exit -> Act 3 -> exit -> Act 4 -> exit -> Act 1 -> exit
The above method will exit all the activities.
上述方法将退出所有活动。
However, if you want to run some code in the background, it is better to rely on a "Service" rather than an "Activity". You can let the "Service" exit after doing its assigned work.
但是,如果您想在后台运行一些代码,最好依赖于“服务”而不是“活动”。您可以在完成分配的工作后让“服务”退出。
回答by user2955481
Do a
<code>
try
{
finalize();
finish
}
catch(Exception error)
{
error.printStackTrace();
}
//This would exit your App neatly
</code>
回答by Hakim Douib
I use this method :
1. Create static activity in the first activity that will close the
remaining activity(s).
2. Then call it in the static method like this, it will close the app from wherever you call it.
我使用这种方法:
1. 在第一个活动中创建静态活动,将关闭剩余的活动。
2.然后像这样在静态方法中调用它,它会从你调用它的任何地方关闭应用程序。
public static Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity=this;
}
public static void CloseApp(){
activity.finish();
}
回答by Jithin Jude
Use android:noHistory = "true"
in your AndroidManifest.xml file
使用android:noHistory = "true"
在AndroidManifest.xml档案
<activity
android:name=".Act1"
android:noHistory="true" />
<activity
android:name=".Act2"
android:noHistory="true" />
<activity
android:name=".Act3"
android:noHistory="true" />
<activity
android:name=".Act4"
android:noHistory="true" />