Question

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");

    }
}
Was it helpful?

Solution

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?

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top