-1

I have an Android app with 2 primary activities. When the app starts from scratch, both activities start and run just fine. Something like: A -> B. Activity A does all of the initialization needed for both A & B. All of my local testing on real hardware and emulator, A is always created (onCreate) before B is created.

However, on my app's crash report, I see an exception which can only be explained by B being started without or before A. Is this possible? Will Android create an internal Activity without creating the other activities for my app (B without A)? Is the order of Activity creation guaranteed (A then B)? How would I re-create either of these scenarios using the emulator or real hardware?

I can easily move my initialization code to work regardless of which Activity is started first, BUT I wanted to learn how to reproduce and test before making changes. I looked through the documentation but it didn't really help.

Here is the code that starts task B when user presses "play" button:

private void handlePlayTouch()
{
    Intent intent = new Intent(getApplicationContext(), PlayActivity.class);
    startActivity(intent);      
}
raider33
  • 1,633
  • 1
  • 19
  • 21
  • If you're resuming the application after pressing the home button (and perhaps after onStop() is called) it won't resume the original state of Activity A. In any case, each activity ought to be whole unto itself. – Estel Dec 11 '12 at 16:49
  • How do you start two activites at once? Show us some code! – theomega Dec 11 '12 at 16:50
  • Activity B is started when user presses a button, which navigates the application to the "main screen" from the "home screen". – raider33 Dec 11 '12 at 19:02
  • http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for – Yaqub Ahmad Dec 20 '12 at 18:13

2 Answers2

1

After you started Activity B, you press home button and make your application in the background. The system would kill your application if the free memory is very low. If you tried to switch to your application after your application killed, the system would try to restore your application and activity B without create A first.

You can use DDMMS's Devices view to manually stop your application, there's a red "stop process" button.Make sure that you should make your application in the background.

Michelle
  • 1,097
  • 8
  • 18
  • I've never heard of DDMS, so thanks for the information. I'll try using it and, if I can reproduce the issue, will accept this as the answer. – raider33 Dec 12 '12 at 15:22
  • I ran DDMS and was able to reproduce the error exactly like the stack trace seen in the crash report. And, once I moved the initialization code, everything works fine now. – raider33 Dec 13 '12 at 03:16
0

In your case, I suggest you using a single activity. In the on create, you can prepare everything and then do what you need. If you would like to be sure that something will be executed only when something else is finished, use AsyncTasks.

In doInBackground => do the initialisation and onPostExecute, do what you have to do after.

The onPostExecute will bee executed only when the doInBackground has finished.

http://developer.android.com/reference/android/os/AsyncTask.html

EDIT:

Your structure is not respecting a good programming practice in Android, but if it is mandatory for you to keep this structure, you should at least use "unkillable" services for the activity A. This will make your code harder to destroy as a simple activity when your app will be put on the background, but there is still a chance to be destroyed.

To understand your problem, see the android activity life cycle:

enter image description here

For the services using, see:

http://developer.android.com/reference/android/app/Service.html

http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
  • I'd like to keep the structure of the activities as they currently are. Like the post says, I don't mind moving the initialization code so that either Activity can initialize itself. Mainly I wanted to know for sure if B could be restarted without A. – raider33 Dec 11 '12 at 20:46
  • Oj, see the edited answer. Without any code provided, I am not able to give you more details. – Milos Cuculovic Dec 12 '12 at 08:01