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