0

I have a MainActivity class, which launches a RaceActivity at some point. If, later, the RaceActivity exits, I want the MainActivity to return, with all its views. However, it seems to be created anew each time. I have implemented the proper section of onOptionsItemSelected, but when I click the back button, I get a new instance each time, or at least the programmatically added views are gone. What can I do to fix this?

Edit for clarification:

I am fine using onCreate with a bundle to restore these views, but I thought that happened automatically if you recreate the same instance of an object. I want to keep programmatically created views when the activity is recreated. Alternatively, I want the activity to stop being destroyed when the user returns to it. (I tested, and it gets destroyed as soon as the use returns.)

Jakob Weisblat
  • 7,450
  • 9
  • 37
  • 65
  • If the system decides that an Activity needs to be destroyed, there is nothing you can do to stop it. If an activity is destroyed, there is nothing you can do to stop its views from being destroyed as well. So... I think that pretty much answers your question right there: it's not possible. – Alex Lockwood Aug 26 '13 at 04:49
  • @AlexLockwood .. try to underdtand his question properly. For him everytime he presses back button, his previous activity is getting recreated. He wants to prevent that and that is possible by setting appropriate flags. System decides to destroy activity if it goes low on memory and a particular activity has lowest priority. I underdtand this cannot be avoided. – Sushil Aug 26 '13 at 05:19
  • @Sushil His question is "How do I prevent the previous activity's views from being recreated", and the answer is "If your application relies on the fact that the previous activity's views need to be recreated, then you're going to have a bad time." As far as I can tell, his question was not "how do I make it less likely that the previous activity will be recreated when I press the back button". If that is indeed the question, then the original post needs to be rephrased to make that more clear. – Alex Lockwood Aug 26 '13 at 06:18
  • I have updated my answer. Hope it helps – jamesc Aug 26 '13 at 14:17

2 Answers2

0

Accept my answer if it helps you. it take hours to find the solution..

override onBackPressed()

Intent intent = new Intent(FromAnyActivity.this, CurrentActivity.class);

            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

            startActivity(intent);

and insert the code in it.. It will reorder your Activity from stack and your views and data will remain where it was. There was another way by adding SingleInstance launch mode.. read these link may help..

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
-2

Add the following to your MainActivity declaration in the AndroidManifest file:

android:launchMode="singleTask"

According to the Android APIs, this addition is for: "The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one."

If the above doesn't work, I believe you can also try adding this as well to the MainActivity in your AndroidManifest file:

android:alwaysRetainTaskState="true"

According to the Android APIs, this addition is for: "Whether or not the state of the task that the activity is in will always be maintained by the system — "true" if it will be, and "false" if the system is allowed to reset the task to its initial state in certain situations. The default value is "false". This attribute is meaningful only for the root activity of a task; it's ignored for all other activities."

Artail3
  • 272
  • 3
  • 6
  • That's just plain wrong and will not prevent the Android OS from destroying and re-creating the activity. – jamesc Aug 26 '13 at 03:32
  • @jamesw .. can you please explain the purpose of "singleTask" flag then?? and see the android source code. Even google guys have used it in multiple places for such scenarios. You need to get your basics right.. – Sushil Aug 26 '13 at 03:59
  • @Sushil The singleTask flag is there to ensure that there is only one instance of the task running rather than multiple instances of the task. See this answer for a good explanation http://stackoverflow.com/questions/11668144/about-android-launchmode-singletask – jamesc Aug 26 '13 at 04:06
  • @Sushil If you really believe that you can prevent the Android system from destroying an activity whenever it wants to, you have a nasty shock coming. – jamesc Aug 26 '13 at 04:11
  • I dont believe that I can prevent Android system from destroying an activity. What I just wanted to say that its not a good idea to create views everytime. Okay. I will give you a scenario. Take a music player which is playing music. Now it you let it getting destroyed and recreated, music will stop for a second which is not good. – Sushil Aug 26 '13 at 04:18
  • Or if its a complicated view whihc takes time to load, if we recreate it everytime, it will just make application slow – Sushil Aug 26 '13 at 04:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36216/discussion-between-jamesw-and-sushil) – jamesc Aug 26 '13 at 04:37
  • You can't prevent the Activity from being destroyed. The Android system will kill whenever it wants whether you use singleTask or not. – Alex Lockwood Aug 26 '13 at 04:51