How to return to last activity after recents screen

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to return to last activity after recents screen



I have an App with a main activity and a popup activity, if I stay in the app there is no problem. But if the popup activity is open and you re-enter the app from the recents screen only the popup activity is shown. If you then close it you don't return to the main activity but the app closes completely.



This opens the popup activity from the main activity:


intent = new Intent(ctx, popupActivity.class);
intent.putExtra("rackName", resultChest.get(2));

ctx.startActivity(intent);



And here is the method which closes the popup activity:


@Override
public boolean onTouchEvent(MotionEvent event)
finish();
return super.onTouchEvent(event);



The AndroidManifest.xml:


<application
android:allowBackup="true"
android:icon="@mipmap/ic_lchr"
android:logo="@mipmap/ic_lchr"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_lchr_round"
android:supportsRtl="true">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".popupActivity"
android:theme="@style/popupTheme"
android:launchMode = "singleInstance">
</activity>
</application>



Solution:

as @RedWolf mentioned android:launchMode = "singleInstance" causes the activity to open in another task which causes my problem.

To resolve this and prevent the popup activity to open twice I had to use android:launchMode = "singleTop" for the popup activity.


android:launchMode = "singleInstance"


android:launchMode = "singleTop"





Show AndroidManifest.xml file as well.
– VicJordan
Aug 13 at 7:54


AndroidManifest.xml





@VicJordan sure
– J.Doe
Aug 13 at 8:10




2 Answers
2



Do not use android:launchMode = "singleInstance", it will create a new task.


android:launchMode = "singleInstance"





oh great to know, using android:launchMode = "standard", solves my problem. But the reason i was using singleInstance was to prevent the prevent the popupActivity from opening twice. And with android:launchMode = "standard" it again openes twice.
– J.Doe
Aug 13 at 9:19





Did you try android:launchMode = "singleTop" ?
– Lester L.
Aug 13 at 9:33




You can do it like this



Go to popupActivity.java then
Override the method named OnBackPressed
And add finish(); to it



Code


@Override
public void onBackPressed()
finish();





Tho I would be better able to answer the question if you could upload the screenshot of the problem.
– Yashaswi Ratan
Aug 13 at 8:37





if you press back the popup activity closes but not if you acces the recents screen, and the problem stays the same.
– J.Doe
Aug 13 at 8:38





This should work to make sure finish leads to previous activity Add parent activity in manifest like this <activity android:name=".popupActivity" android:theme="@style/popupTheme" android:parentActivityName="com.example.myfirstapp.MainActivity" android:launchMode = "singleInstance">
– Yashaswi Ratan
Aug 14 at 9:33







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard