Complementing the response of Erasmus and adding the attribute parentActivityName
, also add the attribute launchMode
in the block that is intended for the main screen. I will put a code below better describing my answer.
Where the manisfest
of its application:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exemplo.ExemploDeCodigo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity
android:name=".SegundaActivity"
android:parentActivityName=".MainActivity" >
</activity>
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Each time a new Activity is added to your project, you add the parentActivityName
pointing to the Activity that you wish to return.
Another interesting point is that if you don’t want to lose any data from a screen and you want it to stay the same by moving to the next screen and then returning, just use the android:launchMode="singleTop"
, this will cause the data to be kept.