Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you call the main launcher activity from another activity?

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?

like image 353
user1332680 Avatar asked Apr 14 '12 00:04

user1332680


2 Answers

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.

like image 106
Shankar Agarwal Avatar answered Nov 15 '22 05:11

Shankar Agarwal


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

like image 27
Mark Pazon Avatar answered Nov 15 '22 05:11

Mark Pazon