0

I have activity A and activity B. When app opens activity A opens.

I go to A to B. Then how do you "go back to A from B", without destroying B so that when you go from A to B the process is super fast? (Also how would you have to open old B and not create a new B?)

Also one more question, when you "go back to A from B", is there a way you prevent B to show up when you press back button once you are back in A?

coolcool1994
  • 3,704
  • 4
  • 39
  • 43

2 Answers2

1
  1. When creating B, do all the hard work in onCreate, so later when resuming it, it starts up faster. B is destroyed only when it's not used and android needs to free some memory. Since it's in the background, switching to it will be pretty fast. To speed it up you can switch without an animation:

        Intent intent = new Intent(this, B.class);
        startActivity(intent);
        overridePendingTransition(0, 0);
    
  2. To prevent multiple B activities to be created, in your manifest's B activity tags set:

    android:launchMode="singleTask"
    
  3. After you go back from B to A, going back again, will quit the app. That is unless you go to A explicitly. Then you will want to override onBackPressed() in activity A, that always quits the app instead of going back to B.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • 1) It seems like my Activity B is being created each time, and not resumed. I am going from A to B and B to A explicitly using Intent intent - new intent... startActivity because I have extra data I need to send in both times with putExtra. Do you know how to just resume Activity B without creating it each time B is opened? B is singleTask. 3) how do you quit the app entirely programmatically? finish() just quit that activity. – coolcool1994 Nov 17 '14 at 01:17
  • The thing is in my app when I go from B to A, pressing back button just quits the app. I don't know why, but it should go to B instead.. I think B is being destroyed each time, but I didn't call finish() in B, instead i just startActivity to activity A. – coolcool1994 Nov 17 '14 at 01:20
  • I guess you can exit the app by moveTaskToBack (true); – coolcool1994 Nov 17 '14 at 01:26
0

Think you need to read this: http://developer.android.com/training/implementing-navigation/ancestral.html

If you want to always exit your app when pressing back in A, then you can override 'onBackPressed'.

Regarding how you would keep A 'alive' so to speak, you could set A to be 'singleInstance' or 'singleTask' in your manifest Android singleTask or singleInstance launch mode?

Community
  • 1
  • 1
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33