如何返回到Android的主要活动? [英] how back to the main activity in android?
问题描述
我有一个 MainActivity
A,其中有一个按钮,启动活动B和活动B还有一个按钮启动活动C。在活动B和C,都有 onBack pressed()
方法,它会出现一个 AlertDialog
问用户想要恢复到 MainActivity
。如果他们preSS是,该计划应显示MainActiviy。
I have a MainActivity
"A", which have a button that launch activity "B", and activity "B" have another button that launch activity "C". In the activities B and C, both have onBackPressed()
method, which appears an AlertDialog
asking if the user wants to return to MainActivity
. If they press yes, the program should show MainActiviy.
现在的问题是:在活动B,我有没有问题,只需拨打完成()
方法,以及 MainActivity
出现,但问题是在活动C,如果我称之为完成()
方法,程序返回到活动B.如何回 MainActivity
从活动C 14
The question is: in activity B, i have not problem, simply call the finish()
method, and MainActivity
appears, but the problem is in activity C, if i call a finish()
method, the program is back to activity B. How back to MainActivity
from activity C??
推荐答案
试试这个你onBack pressed:
try this on you onBackPressed:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
这清除了活动堆栈并打开您的主要活动。不管你是哪个活动,你会回去的主要活动和其他所有活动都从堆栈中移除。
this clears the activity stack and opens your main activity. No matter which activity you are, you will always go back to the main activity and all other activities are removed from the stack.
这篇关于如何返回到Android的主要活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!