3

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.

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??

4

3 回答 3

5

在你身上试试这个 onBackPressed:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

这将清除活动堆栈并打开您的主要活动。无论您是哪个活动,您都将始终返回主活动,并且所有其他活动都从堆栈中删除。

于 2015-10-21T05:19:34.010 回答
3

在活动 B 和 C 的清单文件中使用以下内容:

<activity android:name=".ActivityB"
            android:parentActivityName=".MainActivity"/>
<activity android:name=".ActivityC"
            android:parentActivityName=".MainActivity"/>
于 2015-10-21T00:36:53.533 回答
0

您可以尝试 ActivityCompat.finishAffinity 并在您的 Activity C 处启动 MainActivity。

于 2015-10-21T01:18:35.913 回答