Android: restore activity when resuming application

I have two actions: mainActivity and childActivity. Whenever the user clicks a button in mainActivity, childActivity is triggered. I want to do the following:

When the active action is childActivity and the user clicks the home button and then re-launches the application , I want to see the initial activity of childActivity instead of mainActivity.

I had some suggestions that really work. I tried to manipulate onStart, onRestart, onResume, onStop, onDestroy events. But they did not completely solve the problem. There must be a smart way. Thanks.

Edit:

Thanks for the answer, Sonil. The case you said occurs when an action is called from the last action window. (the window opens when you press the home button for a long time). This does not happen when you open it from the main screen. (like opening from the beginning) I don't think my code has a specific problem for creating this error. Because I created a test project and tried a standalone process before submitting a question and ran into the same problem. Anyway, 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"); } } 
+6
android android-activity
source share
2 answers

EDIT : Found a solution to your problem in a somewhat random fashion today! See this bug report report . This exactly explains your problem. The reason I could not reproduce the problem is because Eclipse is not launching the application directly. I use Eclipse to install the application and then run it myself.


This is already the default behavior for Android applications; special tricks are not required to achieve this. I am surprised that your application does not exhibit this behavior. Each Android application supports an activity stack, literally represents a LIFO action stack. These actions can be grouped into tasks, but 99% of worldly applications will never know anything about tasks in my experience.

When you press the home button, the entire application stack is placed in the background. Although in the background it can be killed for memory problems at any time, but if it does not take a long time to restore it, it usually does not kill and does not need to be re-created. When you select an application again, the stack is restored (or, more precisely, only the top item in the stack).

If your application does not exhibit this behavior, I suspect it has something to do with how you start mainActivity and childActivity, and any additional Intent flags that you can use. Can you post code snippets about how you start mainActivity and childActivity?

+3
source share

I came back and tested using a similar application, and even when the process is pushed out of memory, ChildActivity is automatically restored, as Swanil says. Do you see this on the emulator or on the device itself?

If you run your application and watch the logarithm, after starting the application you will see the following: then open ChildActivity and click "Home", and then start your activity again:

Initial 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 print the output of logcat when you don't see the expected behavior?

-one
source share

All Articles