views:

2188

answers:

3

I have two activities running: mainActivity and childActivity. Whenever the user clicks in the button in mainActivity, the childActivity is launched. What I want to do is this:

When the active activity is the childActivity and the user clicks the home button then relaunch the application, I want to see the childActivity instead of mainActivity that is launched.

I had some suggestions actually work arounds. I tried to manipulate onStart, onRestart, onResume, onStop, onDestroy events. But, they didn't fully solve the problem. There should be a smart way out there. Thank you.

Edit:

Thank you for the answer, Soonil. The case you said is happening when the activity is called from recent activities window. (the window opened when you long press the home button) However; This is not happening when you open it from home screen. (like opening from start) I don't think my code has a specific problem to generate this error. Because, I created a test project and tried standalone before sending the question and faced the same problem. Anyhow, here is the test code:

public class MainActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     this.setTitle("MainActivity");

     ((Button) findViewById(R.id.btnChildActivity)).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
     // TODO Auto-generated method stub
     startActivity(new Intent(this, ChildActivity.class));
    }

}

public class ChildActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main2);
     this.setTitle("ChildActivity");

    }
}
+2  A: 

EDIT: Found the solution to your problem somewhat randomly today! See this error report. It explains your problem exactly. The reason I couldn't reproduce the problem is I never have Eclipse launch an app directly. I use Eclipse to install the app and then start it myself.


This is already the default behavior for Android applications, no special tricks are required to achieve this. I'm surprised your application isn't demonstrating this behavior. Every Android application maintains an Activity stack, literally a LIFO stack of activities. These activities can be further grouped into tasks, but 99% of mundane apps won't ever need to know anything about tasks in my experience.

When you press the home button, the entire application stack is put into the background. While in the background, it may be killed for memory concerns at any time, but if not much time elapses before it is restored, it generally isn't killed and doesn't have to be recreated. When you select the application again, the stack (or more accurately, only the top item on the stack) is restored.

If your application isn't exhibiting this behavior, I suspect it has something to do with how you are starting the mainActivity and childActivity and any extra Intent flags you may be using. Any chance you can post code snippets on how you are starting the mainActivity and childActivity?

Soonil
You are right. After I signed my test application and install it to G1 and run, I saw the correct behavior I am expecting to see. The next I am going to do is to clean my code from save/restore states and rerun on my device. Thank you very much for your help. :)
Omer
Soonil, I want to ask you non related question. Do you use automated script to build and export your application? If so, how do you do that? I couldn't figure out the exporting specifically for android. Thank you.
Omer
I've considered moving to use ANT to build with, but at the moment Eclipse does everything I need. I just use a few scripts to make signing an app a bit easier. You can find a pretty good explanation of a simplified release process at http://developer.android.com/guide/publishing/app-signing.html
Soonil
Thank you so much for your post and edit Soonil, I've been scouring the web and the android application fundamentals for about an hour trying to figure out why my app wouldn't keep it's state on a subsequent launch. The behavior I am experiencing is spelled out exactly in that bug report.
Austyn Mahoney
A: 

I went back and tested with a similar application, and even when the process is forced out of memory the ChildActivity is reconstituted automatically as Soonil says. Are you seeing this on the emulator or on an actual device?

If you run your app and watch logcat, you should see something like the following after you launch your App, then open the ChildActivity and click Home and then launch your activity again:

Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.categroy.LAUNCHER} flags=... comp={com.yourpackagename.MainActivity} } Start proc for activity yourpackagename.ChildActivity: pid=x uid=y gids={} Displayed activity /.ChildActivity

Could you post the output of logcat when you do not see the behavior you expect?

dar
AFAIK this is inaccurate. When a process is killed and it's Activities are restarted the activity stack is saved. Each activity must serialize information only about it's own state. State of the overall activity stack is maintained by the system. (I'll remove my downvote if you can link me to proof)
Soonil
+1  A: 

Check how I workarounded this - http://github.com/cleverua/android_startup_activity

Arhimed