In my program I have an activity that gets launched when the application opens. If I open a couple more activities, how can I go back to the main activity? In the intent filter, the name of the activity is "android.intent.action.MAIN", and it will not allow me to call startActivity() on it. What do I do?
You can do this through an Intent.
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
This Intent will start the launcher application that the user has defined. Be careful with this because this will look like your application crashed if the user does not expect this.
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Let's say your activity stack is as follows... MainActivity > Activity1 > Activity2> Activity3, Running the code above will close activities 1 & 2 and resume MainActivity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With