0

I am facing a problem right now. When I exit the app, and reopen again, it is showing me the previous activity. Is there any way I can launch MainActivity after re-opening the app again? I tried using but to no avail:

SecondActivity.this.finish();
Intent intent = new Intent (SecondActivity.this, MainActivity.class);
startActivity(intent);

and

android:noHistory="true"

Please help me thank you.

  • with which activity you are facing this issue. explain with flow to help you better. Also `noHistory` is with which activity. – Mohammad Tauqir Jan 17 '16 at 15:43
  • I'm doing a time extending app. so basically user will have to fill up the fields in the extending activity (SecondActivity) , after filling up, it will redirect user to Mainactivity. I exited the app by having a dialog box with MainActivity.this.finish(); System.exit(0); . However when i reopen the app (i didnt kill the app) it shows the extend page (SecondActivity). By right, when i reopen, it should show me my MainActivity instead of SecondActivity. – redblackwhite Jan 17 '16 at 15:48

1 Answers1

0

Use this:

Intent intent = new Intent (SecondActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

See here Clear the entire history stack and start a new activity on Android

If you want to call finish(), you should do it after a startActivity call, if you do it in any of the lifecycle callbacks, it could be called in diferent moments, and not only when navigating to other activity.

The good thing of noHistory, is that the system takes care of finising the activity in the correct moment (as you can read here, the system will actually call the finish() method for you) so you can be sure that you are not calling it in a bad moment, and everything is safe (probably this doesn't seem important now, but it could be, for instance when working with threads)

Also try to read about Empty process

A process that doesn't hold any active application components. The only reason to keep this kind of process alive is for caching purposes, to improve startup time the next time a component needs to run in it. The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.

An empty process is one that doesn't hold any active application components. The only reason to keep such a process around is as a cache to improve startup time the next time a component of its application needs to run. As such, the system will often kill these processes in order to balance overall system resources between these empty cached processes and the underlying kernel caches.

Read here http://developer.android.com/guide/topics/processes/process-lifecycle.html

Community
  • 1
  • 1
Mohammad Tauqir
  • 1,817
  • 1
  • 18
  • 53