0

I am building an app that connects to a remote server and then lets you do actions on that server, display data, etc.

There is a "Connect" activity that lets you type the IP address of the server and has a Connect button. Once connected, there are activities B, C, D that are independent and act on different aspects of the server.

If you click the "menu" button, it allows you navigate to any of the activities. If you are disconnected, you can only navigate to the "Connect" activity... the other menu items are disabled.

Here are use cases for how I want the "back" button and task "back stack" behavior to work:

  1. When the app first starts, open the Connect activity
  2. If the app crashes (God forbid), start the Connect activity. Do not try to return to B, C, or D!
  3. If viewing B, C, or D and the connection is lost, "back" button takes you directly to the Connect activity.
  4. If your stack looks like Connect->B->C->D, then you navigate to the Connect screen and disconnect, your stack changes to Connect (only). You cannot hit "back" and go back to B, C, or D
  5. If you leave the app (click "Home" button) and come back by clicking the app icon, it should return to the top of the stack (assuming the connection was not lost in the meantime).
  6. If you are connected, "back" takes you to the previous Activity. Ex. assume Connect->B->C->D->Connect->C->B. One "back" takes you to "C", another "back" to Connect, another "back" to D (i.e. a normal stack).

Any recommendations on how to do this? Especially item 2 above (giving me a big headache... and yes, crashing should not happen)

I have read the Tasks and Back Stack dev guide, but can't decide how all the launch modes and affinities could be applied to this situation.

jfritz42
  • 5,913
  • 5
  • 50
  • 66
  • Linking similar questions: http://stackoverflow.com/questions/6609045/android-back-button-problem http://stackoverflow.com/questions/7618751/remove-state-maintenance-of-screens-android http://stackoverflow.com/questions/5794506/android-clear-the-back-stack http://stackoverflow.com/questions/6794091/clearing-the-android-back-stack – jfritz42 Mar 19 '12 at 16:35

1 Answers1

0

Write a class,

 class MyApplication extends Application{

...

public static boolean isConnected;
...

   } 

Now, whenever you get disconnected or connected, set that boolean to true or false accordingly. And if that boolean is ` true then take action accordingly.

E.g.

  if(MyApplication.isConnected){ 
  startActivityB();
 }else{
   startConnectActivity();
 } 

Also remember, to start an activity, you need an activity context, so maintain a static variable of Activity type in MyApplication and set it in onCreate() method of your first ACTIVITY like MyApplication.activityObj= this And then call activities using that context like- MyApplication.activityObj.startActivity(....

Rajkiran
  • 15,845
  • 24
  • 74
  • 114
  • 1
    My problem isn't with how to start Activities, my problem is with the back button behavior. E.g. if an Activity is on the back stack, but the connection has been lost. Also, keeping a static reference to an Activity is a bad idea and will create memory leaks. – jfritz42 Mar 19 '12 at 16:39