I am calling Activity2 from Activity1 through intent and on pressing back button I want my activity2 not to get destroyed and go back to Activity1 and again from activity1 want to start activity2. I have tried to override the back button but then by using moveTaskToBack(true) is taking me back to my phones home screen but i want to come back to previous activity.
Asked
Active
Viewed 53 times
0
-
Use fragments for this purpose – Rahul May 21 '18 at 21:31
-
1Possible duplicate of [Overriding default back button behavior in android](https://stackoverflow.com/questions/36637887/overriding-default-back-button-behavior-in-android) – Ratata Tata May 21 '18 at 21:51
-
I tried to override default back button but then it is taking m e back to home screen but i want to come back to the activity which has called it. – Faizan Khan May 21 '18 at 22:45
1 Answers
0
Activities (and Fragments) are designed to destroy when onBackPressed
is called. Since the user has no intention to proceed interaction with the screen anymore.
I think the best approach of your navigation should be use of Fragments with manually handled navigation. When Activity
starts initialize both Fragment
s. When you'll need to open second screen, just replace current fragment with Fragment2
. When onBackPressed
is called - replace Fragment2
with Fragment1
. In this case both fragments are always available to be shown.
One thing to be aware of - state saving, since the system can destroy your app in the background anyway. In this case, save state manually when Activity
is being destroyed.

Demigod
- 5,073
- 3
- 31
- 49