So, in short my app has the following startup procedure:
On launch --> Starts StartupActivity
StartupActivity --> Starts MyService (startService, long-running)
MyService feeds some text to StartupActivity until MyService has TCP connection to server
MyService then launches the next activity, LoginActivity
LoginActivity sends login packet (via MyService) and when reply is received (in the MyService), MyService launches the next Activity, MainActivity. So:
StartupActivity --> LoginActivity --> MainActivity
|
+---> MyService
The MainActivity is then the only activity present.
I have several problems here:
1)
If I at any point in time press the HOME-button and then want to return to the app (by clicking the app icon) I want to return to whatever Activity was visible before the HOME-button was pressed. It can be StartupActivity, LoginActivity or MainActivity.
However, if I press the HOME-button when I am on the LoginActivity, I am always returned to the StartupActivity (even though that has done a finish() and LoginActivity was started with *Intent.FLAG_ACTIVITY_NEW_TASK*)
2) I have also noticed that the onCreate of the StartupActivity is always executed when I return to the app (as described in 1 above). I expected the StartupActivity.onCreate to be executed once only (unless it has been destroyed, which it hasn't in this case). When I press the launch icon, the onCreate is executed and after that, the onDestroy. I have no idea why.
3) What is the best approach to force an app into a Activity that should be the "current" one? THe launcher is StartupActivity, but in most cases I would want LoginActivity or MainActivity to be shown (if we have come to that state).
Any tips and pointers would be appreciated =)
I was thinking if I should post some code, but there isnt anything in there that is weird or strange or even advanced. So I will just post some pieces so you have it in front of you.
MyService.onCreate
public void onCreate(){
super.onCreate();
SRef.clientHandlerLS = new ClientHandler(); // my TCP connection
startupProgressIntent = new Intent("STARTUP_PROGRESS_MESSAGE");
loginProgressIntent = new Intent("LOGIN_PROGRESS_MESSAGE");
mainIntent = new Intent("MAIN_MESSAGE");
ANDROID_ID = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
addTextToStartup("Sometext...\n");
loginHandler = new Handler();
startupHandler = new Handler();
createNotification("Starting...", "Starting...", true);
SRef.myService = myBinder;
startupHandler.postDelayed(startupStuff, 1000);
}
This is how I start the LoginActivity from the MyService
Intent myIntent = new Intent(MyService.this, LoginActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyService.this.startActivity(myIntent);
StartupActivity.onCreate
@Override
protected void onCreate(android.os.Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.startup);
tv = (TextView)findViewById(R.id.textViewStartupData);
tv.append("StartupActivity.onCreate\n");
registerReceiver(connectionReceiver, new IntentFilter("STARTUP_PROGRESS_MESSAGE"));
registerReceiver(connectionReceiver, new IntentFilter("STARTUP_FINISH"));
registerReceiver(connectionReceiver, new IntentFilter("SERVICE_CONNECTED"));
startService(new Intent(StartupActivity.this, MyService.class));
}