how back to the main activity in android?

江枫思渺然 提交于 2019-12-05 18:31:20

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.

Use the following in your manifest file for the activity B and C:

<activity android:name=".ActivityB"
            android:parentActivityName=".MainActivity"/>
<activity android:name=".ActivityC"
            android:parentActivityName=".MainActivity"/>

You can try ActivityCompat.finishAffinity and start MainActivity at your Activity C.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!