Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Restoring the active Activity while resuming the application

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

    }
}
like image 807
Ömer Avatar asked Apr 07 '09 07:04

Ömer


1 Answers

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?

like image 175
sooniln Avatar answered Dec 01 '22 06:12

sooniln