How does the android program exit the current activity?

Source: Internet
Author: User

The android program has a lot of activities, such as the main window A, and calls the sub-window B. If you finish () directly in B, the next display is. In B, how does one close the entire android application? I have summarized several simple implementation methods.

1. Local Dalvik VM Method

Android. OS. process. killprocess (Android. OS. process. mypid () // obtain the PID.

System. Exit (0); // standard exit method of general Java and C #. If the returned value is 0, the system Exits normally.

2. Task Manager Method

First, it must be noted that this method runs on Android 1.5 apilevel 3 or above and requires permissions.

Activitymanager AM = (activitymanager) getsystemservice (context. activity_service );

Am. restartpackage (getpackagename ());

The system will kill all processes, services, and services under the package, and you will be able to kill them.

3. According to the activity declaration cycle

3. we know that the window class of Android provides a historical stack, and we can implement it skillfully through the stack principle. Here we add the intent icon directly to window a when window a opens window B. flag_activity_clear_top. When B is enabled, all activities in the process space are cleared.

Use the following code to call window B in window:

Intent intent = newintent ();

Intent. setclass (android123.this, cwj. Class );

Intent. setflags (intent. flag_activity_clear_top); // pay attention to the flag settings of the row.

Startactivity (intent );

Next, use the finish method to exit all the services in window B.

4. customize an actiivty stack. The same principle is true. However, an activity stack in singleton mode is used to manage all activities. And provides the method to exit all activities. The Code is as follows:

Public classscreenmanager {

Private staticstack activitystack;

Private staticscreenmanager instance;

Privatescreenmanager (){

}

Public staticscreenmanager getscreenmanager (){

Instance = newscreenmanager ();

}

Return instance;

}

// Exit the stack top Activity

Public voidpopactivity (activity ){

Activity. Finish ();

Activitystack. Remove (activity );

Activity = NULL;

}

}

// Obtain the activity at the top of the current stack

Public activitycurrentactivity (){

Activityactivity = activitystack. lastelement ();

Return activity;

}

// Push the current activity into the stack

Public voidpushactivity (activity ){

Activitystack = newstack ();

}

Activitystack. Add (activity );

}

// Exit all activities in the stack

Public voidpopallactivityeffectone (class CLs ){

While (true ){

Activityactivity = currentactivity ();

Break;

}

Break;

}

Popactivity (activity );

}

}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.