จะปิดแอปพลิเคชัน Android ได้อย่างไร


157

ฉันต้องการปิดแอปพลิเคชันของฉันเพื่อไม่ให้ทำงานในพื้นหลังอีกต่อไป

ทำอย่างไร นี่เป็นแนวปฏิบัติที่ดีบนแพลตฟอร์ม Android หรือไม่

ถ้าฉันใช้ปุ่ม "ย้อนกลับ" มันจะปิดแอพ แต่จะอยู่ในพื้นหลัง มีแอปพลิเคชันที่เรียกว่า "TaskKiller" เพียงเพื่อฆ่าแอปเหล่านั้นในพื้นหลัง


4
คำถามนี้ถูกถามไปแล้ว ลองดูที่stackoverflow.com/questions/2033914/ …หรือstackoverflow.com/questions/2042222/android-close-application
Dave Webb

สงสัยว่าทำไมคนไม่ต้องการให้แอปของเขาทำงานแม้ในพื้นหลัง
Darpan

คำตอบ:


139

Android มีกลไกในการปิดแอปพลิเคชันอย่างปลอดภัยตามเอกสารของมัน ในกิจกรรมล่าสุดที่ออก (โดยปกติคือกิจกรรมหลักที่เกิดขึ้นครั้งแรกเมื่อแอปพลิเคชันเริ่มต้น) เพียงวางสองบรรทัดในเมธอด onDestroy () การเรียกใช้System.runFinalizersOnExit (true)ทำให้แน่ใจว่าวัตถุทั้งหมดจะถูกสรุปและรวบรวมขยะเมื่อแอปพลิเคชันออก นอกจากนี้คุณยังสามารถฆ่าแอปพลิเคชันได้อย่างรวดเร็วผ่านandroid.os.Process.killProcess (android.os.Process.myPid ()) หากคุณต้องการ วิธีที่ดีที่สุดในการทำเช่นนี้คือใส่วิธีการดังต่อไปนี้ในคลาสผู้ช่วยแล้วเรียกมันว่าเมื่อใดก็ตามที่แอปจำเป็นต้องถูกฆ่า ตัวอย่างเช่นในวิธีการทำลายของกิจกรรมรูท (สมมติว่าแอปไม่เคยฆ่ากิจกรรมนี้):

Android จะไม่แจ้งเตือนแอปพลิเคชันของเหตุการณ์ปุ่มHOMEดังนั้นคุณจึงไม่สามารถปิดแอปพลิเคชันได้เมื่อกดปุ่มHOME Android ขอสงวน ปุ่มหน้าแรกด้วยตนเองเพื่อให้ผู้พัฒนาไม่สามารถป้องกันผู้ใช้ออกจากแอปพลิเคชันได้ อย่างไรก็ตามคุณสามารถกำหนดได้ด้วยการกดปุ่มHOMEโดยการตั้งค่าสถานะเป็นจริงในคลาสผู้ช่วยซึ่งถือว่ามีการกดปุ่มHOMEจากนั้นเปลี่ยนค่าสถานะเป็นเท็จเมื่อมีเหตุการณ์เกิดขึ้นซึ่งแสดงว่าปุ่มHOMEไม่ได้ถูกกด การตรวจสอบเพื่อดูปุ่มโฮมที่กดในวิธีonStop ()ของกิจกรรม

อย่าลืมจัดการกับปุ่มHOMEสำหรับเมนูใด ๆ และในกิจกรรมที่เริ่มโดยเมนู เช่นเดียวกันกับปุ่มSEARCH ด้านล่างเป็นตัวอย่างคลาสที่จะแสดง:

นี่คือตัวอย่างของกิจกรรมรูทที่ฆ่าแอปพลิเคชันเมื่อมันถูกทำลาย:

package android.example;

/**
 * @author Danny Remington - MacroSolve
 */

public class HomeKey extends CustomActivity {

    public void onDestroy() {
        super.onDestroy();

        /*
         * Kill application when the root activity is killed.
         */
        UIHelper.killApp(true);
    }

}

ต่อไปนี้เป็นกิจกรรมนามธรรมที่สามารถขยายเพื่อจัดการคีย์HOMEสำหรับกิจกรรมทั้งหมดที่ขยาย:

package android.example;

/**
 * @author Danny Remington - MacroSolve
 */

import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;

/**
 * Activity that includes custom behavior shared across the application. For
 * example, bringing up a menu with the settings icon when the menu button is
 * pressed by the user and then starting the settings activity when the user
 * clicks on the settings icon.
 */
public abstract class CustomActivity extends Activity {
    public void onStart() {
        super.onStart();

        /*
         * Check if the app was just launched. If the app was just launched then
         * assume that the HOME key will be pressed next unless a navigation
         * event by the user or the app occurs. Otherwise the user or the app
         * navigated to this activity so the HOME key was not pressed.
         */

        UIHelper.checkJustLaunced();
    }

    public void finish() {
        /*
         * This can only invoked by the user or the app finishing the activity
         * by navigating from the activity so the HOME key was not pressed.
         */
        UIHelper.homeKeyPressed = false;
        super.finish();
    }

    public void onStop() {
        super.onStop();

        /*
         * Check if the HOME key was pressed. If the HOME key was pressed then
         * the app will be killed. Otherwise the user or the app is navigating
         * away from this activity so assume that the HOME key will be pressed
         * next unless a navigation event by the user or the app occurs.
         */
        UIHelper.checkHomeKeyPressed(true);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.settings_menu, menu);

        /*
         * Assume that the HOME key will be pressed next unless a navigation
         * event by the user or the app occurs.
         */
        UIHelper.homeKeyPressed = true;

        return true;
    }

    public boolean onSearchRequested() {
        /*
         * Disable the SEARCH key.
         */
        return false;
    }
}

นี่คือตัวอย่างของหน้าจอเมนูที่จัดการปุ่มHOME :

/**
 * @author Danny Remington - MacroSolve
 */

package android.example;

import android.os.Bundle;
import android.preference.PreferenceActivity;

/**
 * PreferenceActivity for the settings screen.
 * 
 * @see PreferenceActivity
 * 
 */
public class SettingsScreen extends PreferenceActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.settings_screen);
    }

    public void onStart() {
        super.onStart();

        /*
         * This can only invoked by the user or the app starting the activity by
         * navigating to the activity so the HOME key was not pressed.
         */
        UIHelper.homeKeyPressed = false;
    }

    public void finish() {
        /*
         * This can only invoked by the user or the app finishing the activity
         * by navigating from the activity so the HOME key was not pressed.
         */
        UIHelper.homeKeyPressed = false;
        super.finish();
    }

    public void onStop() {
        super.onStop();

        /*
         * Check if the HOME key was pressed. If the HOME key was pressed then
         * the app will be killed either safely or quickly. Otherwise the user
         * or the app is navigating away from the activity so assume that the
         * HOME key will be pressed next unless a navigation event by the user
         * or the app occurs.
         */
        UIHelper.checkHomeKeyPressed(true);
    }

    public boolean onSearchRequested() {
        /*
         * Disable the SEARCH key.
         */
        return false;
    }

}

นี่คือตัวอย่างของคลาสตัวช่วยที่จัดการกับปุ่มHOMEทั่วทั้งแอป:

package android.example;

/**
 * @author Danny Remington - MacroSolve
 *
 */

/**
 * Helper class to help handling of UI.
 */
public class UIHelper {
    public static boolean homeKeyPressed;
    private static boolean justLaunched = true;

    /**
     * Check if the app was just launched. If the app was just launched then
     * assume that the HOME key will be pressed next unless a navigation event
     * by the user or the app occurs. Otherwise the user or the app navigated to
     * the activity so the HOME key was not pressed.
     */
    public static void checkJustLaunced() {
        if (justLaunched) {
            homeKeyPressed = true;
            justLaunched = false;
        } else {
            homeKeyPressed = false;
        }
    }

    /**
     * Check if the HOME key was pressed. If the HOME key was pressed then the
     * app will be killed either safely or quickly. Otherwise the user or the
     * app is navigating away from the activity so assume that the HOME key will
     * be pressed next unless a navigation event by the user or the app occurs.
     * 
     * @param killSafely
     *            Primitive boolean which indicates whether the app should be
     *            killed safely or quickly when the HOME key is pressed.
     * 
     * @see {@link UIHelper.killApp}
     */
    public static void checkHomeKeyPressed(boolean killSafely) {
        if (homeKeyPressed) {
            killApp(true);
        } else {
            homeKeyPressed = true;
        }
    }

    /**
     * Kill the app either safely or quickly. The app is killed safely by
     * killing the virtual machine that the app runs in after finalizing all
     * {@link Object}s created by the app. The app is killed quickly by abruptly
     * killing the process that the virtual machine that runs the app runs in
     * without finalizing all {@link Object}s created by the app. Whether the
     * app is killed safely or quickly the app will be completely created as a
     * new app in a new virtual machine running in a new process if the user
     * starts the app again.
     * 
     * <P>
     * <B>NOTE:</B> The app will not be killed until all of its threads have
     * closed if it is killed safely.
     * </P>
     * 
     * <P>
     * <B>NOTE:</B> All threads running under the process will be abruptly
     * killed when the app is killed quickly. This can lead to various issues
     * related to threading. For example, if one of those threads was making
     * multiple related changes to the database, then it may have committed some
     * of those changes but not all of those changes when it was abruptly
     * killed.
     * </P>
     * 
     * @param killSafely
     *            Primitive boolean which indicates whether the app should be
     *            killed safely or quickly. If true then the app will be killed
     *            safely. Otherwise it will be killed quickly.
     */
    public static void killApp(boolean killSafely) {
        if (killSafely) {
            /*
             * Notify the system to finalize and collect all objects of the app
             * on exit so that the virtual machine running the app can be killed
             * by the system without causing issues. NOTE: If this is set to
             * true then the virtual machine will not be killed until all of its
             * threads have closed.
             */
            System.runFinalizersOnExit(true);

            /*
             * Force the system to close the app down completely instead of
             * retaining it in the background. The virtual machine that runs the
             * app will be killed. The app will be completely created as a new
             * app in a new virtual machine running in a new process if the user
             * starts the app again.
             */
            System.exit(0);
        } else {
            /*
             * Alternatively the process that runs the virtual machine could be
             * abruptly killed. This is the quickest way to remove the app from
             * the device but it could cause problems since resources will not
             * be finalized first. For example, all threads running under the
             * process will be abruptly killed when the process is abruptly
             * killed. If one of those threads was making multiple related
             * changes to the database, then it may have committed some of those
             * changes but not all of those changes when it was abruptly killed.
             */
            android.os.Process.killProcess(android.os.Process.myPid());
        }

    }
}

1
นี่ควรจะฆ่าแอปพลิเคชันทั้งหมดที่เรียกว่า System.exit (0) รวมถึงกิจกรรมทั้งหมดที่ใช้งานเป็นส่วนหนึ่งของแอปพลิเคชัน แอปพลิเคชันอื่น ๆ ทั้งหมดจะยังคงทำงานต่อไป หากคุณต้องการฆ่าเพียงกิจกรรมเดียวในแอปพลิเคชัน แต่ไม่ใช่กิจกรรมทั้งหมดในแอปพลิเคชันคุณจะต้องเรียกเมธอด finish () ของกิจกรรมที่คุณต้องการฆ่า
Danny Remington - OMS

2
ขอบคุณมากสำหรับ nfo นี้ ฉันกำลังสร้างเกมกับ AndEngine และเมื่อฉันจะเรียกเสร็จแม้ในทุกกิจกรรม Android จะยังไม่ได้ทำความสะอาดอย่างเต็มที่และเมื่อเกมเปิดตัวอีกครั้ง ดังนั้นหลังจากการตรวจสอบคิดว่ามันเป็น AndEngine ฉันก็ตระหนักว่ามันเป็นสิ่งที่ผิดพลาดเพราะ android กำลังพยายามที่จะรักษากระบวนการเมื่อฉันต้องการที่จะออกจากมัน ความคิดเห็นทั้งหมด "โอ้คุณไม่ควรเรียกทางออกมันทำลายประสบการณ์ของผู้ใช้" เป็นเรื่องไร้สาระ แอปพลิเคชั่นสภาพอากาศควรเปิดอยู่ .......

17
ไม่มีแอปพลิเคชันที่ใช้งานจริงควรใช้รหัสนี้ ไม่มีแอปพลิเคชันที่ใช้งานจริงใด ๆ ควรเรียกรหัสใด ๆ ที่แสดงในkillApp()เนื่องจาก Google ระบุว่าจะนำไปสู่พฤติกรรมที่ไม่อาจคาดการณ์ได้
CommonsWare

1
System.runFinalizersOnExit (จริง); วิธีการเลิกใช้แล้วมีวิธีใดอีกวิธีหนึ่งในการปิดแอปพลิเคชันอย่างปลอดภัย (เก็บรวบรวมขยะ)
Ajeesh

1
มันไม่ได้ถูกปฏิเสธในเวลาที่โพสต์ครั้งแรกนี้ เนื่องจาก AP ปัจจุบันเป็น 7 และ API ปัจจุบันตอนนี้คือ 19 จึงอาจมีวิธีอื่นในการทำเช่นนี้
Danny Remington - OMS

68

ใช่! แน่นอนที่สุดคุณสามารถปิดแอปพลิเคชันของคุณเพื่อที่จะไม่ทำงานในพื้นหลังอีกต่อไป เช่นเดียวกับคนอื่น ๆ แสดงความคิดเห็นfinish()เป็นวิธีที่ Google แนะนำซึ่งไม่ได้หมายความว่าโปรแกรมของคุณจะถูกปิด

System.exit(0);

ที่นั่นจะมีการปิดแอปพลิเคชันของคุณโดยไม่ปล่อยให้สิ่งใดทำงานในพื้นหลังอย่างไรก็ตามให้ใช้อย่างชาญฉลาดและอย่าเปิดไฟล์ไว้เปิดฐานข้อมูลจัดการเปิด ฯลฯ สิ่งเหล่านี้มักจะถูกล้างผ่านfinish()คำสั่ง

ฉันเกลียดเมื่อฉันเลือกออกในแอปพลิเคชันและมันไม่ได้ออกจริง ๆ


44
ไม่แนะนำให้ใช้ System.exit () อย่างแน่นอน
CommonsWare

14
ฉันจะไม่เถียงว่ามันไม่ได้เป็นวิธีที่แนะนำ แต่คุณสามารถมอบวิธีแก้ปัญหาที่รับประกันได้ว่าโปรแกรมจะออกจากพื้นหลังทันทีหรือไม่? ถ้าไม่เช่นนั้น System.exit เป็นหนทางไปจนกว่า Google จะให้วิธีการที่ดีกว่า
Cameron McBride

74
ใครเป็นคนตัดสินใจว่าคุณไม่ได้ "ควร" เป็นคนเดียวกับที่สร้างวิธีที่ไม่ได้ออกจริง ๆ หากผู้ใช้ไม่ต้องการปิดแอปพลิเคชันของพวกเขาแอพที่ได้รับความนิยมสูงสุดอันดับที่ 5 จะไม่ใช่นักฆ่างาน ผู้คนต้องการหน่วยความจำที่เพิ่มขึ้นและระบบปฏิบัติการหลักไม่ทำงาน
Cameron McBride

19
เห็นด้วยว่ามันไม่สมควร แต่ได้รับการโหวตมากขึ้นสำหรับการตอบคำถามจริงที่ถาม ฉันรู้สึกเหนื่อยมากที่ได้ยินว่า "คุณไม่อยากทำอย่างนั้น" โดยไม่มีคำอธิบายติดตาม Android เป็นฝันร้ายที่สมบูรณ์เกี่ยวกับเอกสารสำหรับสิ่งต่าง ๆ เหล่านี้เมื่อเทียบกับ iPhone
DougW

11
ไม่มีประโยชน์หน่วยความจำในการใช้ task killer กับ Android Android จะทำลายและล้างแอปพลิเคชันทั้งหมดที่ไม่ได้อยู่ในเบื้องหน้าหากแอปเบื้องหน้าต้องการหน่วยความจำเพิ่มเติม ในบางกรณี Android จะเปิดแอพที่ปิดตัวลงงานใหม่อีกครั้ง Android จะเติมหน่วยความจำที่ไม่ต้องการทั้งหมดด้วยแอปพลิเคชันที่ใช้ล่าสุดเพื่อลดเวลาสลับแอป อย่าสร้างแอพพลิเคชันด้วยปุ่มออก อย่าใช้ผู้จัดการบัญชีบน Android geekfor.me/faq/you-shouldnt-be-using-a-task-killer-with-android android-developers.blogspot.com/2010/04/…
Janusz

23

นี่คือวิธีที่ฉันทำ:

ฉันแค่ใส่

Intent intent = new Intent(Main.this, SOMECLASSNAME.class);
Main.this.startActivityForResult(intent, 0);

ด้านในของวิธีที่เปิดกิจกรรมจากนั้นภายในวิธีของ SOMECLASSNAME ที่ออกแบบมาเพื่อปิดแอพที่ฉันใส่:

setResult(0);
finish();

และฉันใส่สิ่งต่อไปนี้ในชั้นเรียนหลักของฉัน:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == 0) {
        finish();
    }
}

18

เพียงเพื่อตอบคำถามของฉันเองหลังจากเวลานานมาก (ตั้งแต่คอมมอนส์แสดงความคิดเห็นในคำตอบที่นิยมที่สุดบอกว่าเราไม่ควรทำเช่นนี้):

เมื่อฉันต้องการออกจากแอพ:

  1. ฉันเริ่มกิจกรรมแรกของฉัน (ทั้งหน้าจอเริ่มต้นหรือกิจกรรมใดก็ตามที่อยู่ด้านล่างของสแต็กกิจกรรม) ด้วยFLAG_ACTIVITY_CLEAR_TOP(ซึ่งจะออกจากกิจกรรมอื่น ๆ ทั้งหมดที่เริ่มต้นหลังจากนั้นซึ่งหมายถึง - ทั้งหมด) เพียงแค่ทำให้มีกิจกรรมนี้ในกิจกรรมสแต็ก (ไม่เสร็จด้วยเหตุผลบางอย่างล่วงหน้า)
  2. ฉันโทรหาfinish()กิจกรรมนี้

นี่มันใช้งานได้ดีสำหรับฉัน


3
สิ่งนี้ไม่ได้ฆ่าแอปของคุณ มันจะยังคงปรากฏในรายการแอพ ฉันแค่ฆ่าทุกกิจกรรมของคุณ
Joris Weimar

1
FLAG_ACTIVITY_CLEAN_TOP ใช้ไม่ได้กับสมาร์ทโฟน Sony คุณสามารถแก้ไขปัญหาได้โดยเพิ่มแอนดรอยด์: clearTaskOnLaunch = แอตทริบิวต์ "true" ให้กับกิจกรรมที่ AndroidManifest.xml
Rusfearuth

10

เพียงแค่เขียนรหัสนี้ที่ปุ่ม EXIT คลิก

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("LOGOUT", true);
startActivity(intent);

และในเมธอด onCreate ()ของMainActivity.classของคุณเขียนโค้ดด้านล่างเป็นบรรทัดแรก

if (getIntent().getBooleanExtra("LOGOUT", false))
{
    finish();
}

9

ไม่สามารถใช้ API เฟรมเวิร์กได้ มันขึ้นอยู่กับดุลยพินิจของระบบปฏิบัติการ (Android) ในการตัดสินใจเมื่อกระบวนการควรถูกลบหรือยังคงอยู่ในหน่วยความจำ นี่คือเหตุผลด้านประสิทธิภาพ: หากผู้ใช้ตัดสินใจที่จะเปิดใช้งานแอปใหม่แสดงว่ามีอยู่แล้วโดยที่ไม่ต้องโหลดลงในหน่วยความจำ

ไม่เลยไม่เพียงท้อแท้เท่านั้นมันเป็นไปไม่ได้เลย


4
คุณสามารถทำบางสิ่งเช่น Integer z = null; z.intValue (); // คำตอบที่แย่ที่สุด
Joe Plante

6
จริงที่ คุณสามารถทุบโทรศัพท์ของคุณกระแทกกำแพงซึ่งจะเป็นการปิดแอปพลิเคชั่นที่เปิดอยู่ทั้งหมดหากมีแรงกดดันเพียงพอ ฉันยังคงไม่แนะนำ ฉันได้อัพเดทโพสต์ของฉันแล้ว
Matthias

@JoePlante ที่ทิ้งแอปไว้ในพื้นหลังเมื่อคุณเปิดเมนูแอพ ดูเหมือนว่าเป็นไปไม่ได้
peresis ผู้ใช้

8

สำหรับการออกจากแอป:

วิธีที่ 1:

โทรและแทนที่finish(); onDestroy();ใส่รหัสต่อไปนี้ในonDestroy():

System.runFinalizersOnExit(true)

หรือ

android.os.Process.killProcess(android.os.Process.myPid());

วิธีที่ 2:

public void quit() {
    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    System.exit(0);
}

วิธีที่ 3:

Quit();

protected void Quit() {
    super.finish();
}

วิธีที่ 4:

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

if (getIntent().getBooleanExtra("EXIT", false)) {
     finish();
}

วิธีที่ 5:

บางครั้งการโทรfinish()จะออกจากกิจกรรมปัจจุบันเท่านั้นไม่ใช่แอปพลิเคชันทั้งหมด อย่างไรก็ตามมีวิธีแก้ไขปัญหานี้ เวลาที่คุณเริ่มต้นทุกคนเริ่มต้นโดยใช้activity startActivityForResult()เมื่อคุณต้องการปิดแอพทั้งหมดคุณสามารถทำสิ่งต่อไปนี้:

setResult(RESULT_CLOSE_ALL);
finish();

จากนั้นกำหนดกิจกรรมการonActivityResult(...)เรียกกลับของทุกกิจกรรมดังนั้นเมื่อมีการactivityส่งกลับRESULT_CLOSE_ALLค่ามันก็จะเรียกfinish():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(resultCode){
        case RESULT_CLOSE_ALL:{
            setResult(RESULT_CLOSE_ALL);
            finish();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

เจตนาเจตนา = เจตนาใหม่ (getApplicationContext (), LoginActivity.class); intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra ("EXIT" จริง); startActivity (เจตนา); ทำงานได้ดีมาก
hitesh141

ฉันได้เริ่มกิจกรรม A-> B-> C-> D เมื่อกดปุ่มย้อนกลับบนกิจกรรม DI ต้องการไปที่กิจกรรม A. เนื่องจาก A เป็นจุดเริ่มต้นของฉันและดังนั้นในกิจกรรมทั้งหมดที่ด้านบนของ A จะถูกล้างและคุณไม่สามารถกลับไปที่กิจกรรมอื่นจาก A ได้ . @Override บูลีนสาธารณะ onKeyDown (int keyCode เหตุการณ์ KeyEvent) {ถ้า (keyCode == KeyEvent.KEYCODE_BACK) {เจตนา a = เจตนาใหม่ (นี่คือ A.class); a.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity (ก); กลับจริง } คืนค่า super.onKeyDown (keyCode เหตุการณ์); }
hitesh141

5

นี่คือวิธีที่ Windows Mobile ใช้งานได้ ... ดี ... เคย! นี่คือสิ่งที่ Microsoft ต้องพูดในเรื่อง:

http://blogs.msdn.com/windowsmobile/archive/2006/10/05/The-Em Emperor-Has-No-Close.aspx (มันเศร้าไหมที่ฉันจำชื่อของบล็อกโพสต์ได้ตั้งแต่ปี 2549? ฉันพบบทความใน Google โดยการค้นหา "จักรพรรดิไม่มีวันปิด" lol)

ในระยะสั้น:

หากระบบต้องการหน่วยความจำเพิ่มขึ้นขณะที่แอปอยู่ในพื้นหลังระบบจะปิดแอป แต่ถ้าระบบไม่ต้องการหน่วยความจำเพิ่มเติมแอพจะยังคงอยู่ใน RAM และพร้อมที่จะกลับมาอย่างรวดเร็วในครั้งต่อไปที่ผู้ใช้ต้องการ

ความคิดเห็นจำนวนมากในคำถามนี้ที่ O'Reillyแนะนำว่า Android ทำงานในลักษณะเดียวกันมากโดยปิดแอปพลิเคชันที่ไม่ได้ใช้งานมาระยะหนึ่งเมื่อ Android ต้องการหน่วยความจำที่ใช้งานอยู่เท่านั้น

เนื่องจากนี่เป็นคุณสมบัติมาตรฐานจากนั้นการเปลี่ยนพฤติกรรมเป็นปิดอย่างแข็งขันจะเป็นการเปลี่ยนแปลงประสบการณ์ของผู้ใช้ ผู้ใช้หลายคนจะคุ้นเคยกับการเลิกใช้แอพ Android อย่างอ่อนโยนดังนั้นเมื่อพวกเขายกเลิกแอปพลิเคชั่นที่มีความตั้งใจที่จะกลับมาใช้งานหลังจากทำงานอื่น ๆ พวกเขาอาจจะค่อนข้างผิดหวังว่าสถานะของแอปพลิเคชันนั้นถูกรีเซ็ต เพื่อเปิด ฉันจะยึดติดกับพฤติกรรมมาตรฐานเพราะเป็นสิ่งที่คาดหวัง


5

การเรียกfinish()วิธีการในกิจกรรมมีผลต่อกิจกรรมปัจจุบันที่คุณต้องการ


14
ไม่มันไม่ มันเสร็จสิ้นกิจกรรมปัจจุบันไม่ใช่แอปพลิเคชัน หากคุณเสร็จสิ้น () กิจกรรมที่อยู่ด้านล่างสุดในกองงานแอปพลิเคชันของคุณจะปรากฏขึ้นเพื่อออก แต่ Android อาจตัดสินใจที่จะเก็บมันไว้ตราบใดที่มันเหมาะสม
Matthias

อย่างไรก็ตามหากคุณต้องการออกจากแอปพลิเคชันของคุณอย่างสมบูรณ์คุณต้องโทรหาวิธีการสิ้นสุดสำหรับแต่ละกิจกรรมและคิดเกี่ยวกับบริการที่คุณอาจเริ่มต้น ฉันได้แก้ไขคำตอบเริ่มต้นด้วย - ขอโทษสำหรับการละเว้น
r1k0

3

ไม่มีคำตอบทั้งหมดข้างต้นที่ทำงานได้ดีกับแอปของฉัน

นี่คือรหัสการทำงานของฉัน

ที่ปุ่มออกของคุณ:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mainIntent.putExtra("close", true);
startActivity(mainIntent);
finish();

รหัสนั้นคือการปิดกิจกรรมอื่น ๆ และนำ MainActivity มาอยู่ด้านบนของ MainActivity ของคุณ:

if( getIntent().getBooleanExtra("close", false)){
    finish();
}

2

ใส่finish();คำสั่งดังต่อไปนี้:

myIntent.putExtra("key1", editText2.getText().toString());

finish();

LoginActivity.this.startActivity(myIntent);

ในทุกกิจกรรม



2

คัดลอกโค้ดด้านล่างและวางไฟล์ AndroidManifest.xml ในแท็กกิจกรรมแรก

<activity                        
            android:name="com.SplashActivity"
            android:clearTaskOnLaunch="true" 
            android:launchMode="singleTask"
            android:excludeFromRecents="true">              
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"
                />
            </intent-filter>
        </activity>     

เพิ่มรหัสด้านล่างนี้ในแท็กกิจกรรมในไฟล์ AndroidManifest.xml ด้วย

 android:finishOnTaskLaunch="true"

1

ไม่สามารถทำได้ด้วย 2.3 ฉันค้นหามากและลองใช้แอพมากมาย ทางออกที่ดีที่สุดคือการติดตั้งทั้งสอง (ไปที่ taskmanager) และ (รีบูตอย่างรวดเร็ว) เมื่อใช้ร่วมกันมันจะทำงานและจะเพิ่มหน่วยความจำ อีกทางเลือกหนึ่งคือการอัพเกรดเป็น Android ไอศกรีมแซนวิช 4.0.4 ซึ่งอนุญาตการควบคุม (ปิด) แอพ



1

การใช้finishAffinity()อาจเป็นตัวเลือกที่ดีถ้าคุณต้องการปิดกิจกรรมทั้งหมดของแอพ ตามเอกสาร Android -

Finish this activity as well as all activities immediately below it in the current task that have the same affinity.

1
public class CloseAppActivity extends AppCompatActivity
{
    public static final void closeApp(Activity activity)
    {
        Intent intent = new Intent(activity, CloseAppActivity.class);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        finish();
    }
}

และชัดแจ้ง:

<activity
     android:name=".presenter.activity.CloseAppActivity"
     android:noHistory="true"
     android:clearTaskOnLaunch="true"/>

จากนั้นคุณสามารถโทรCloseAppActivity.closeApp(fromActivity)และแอปพลิเคชันจะถูกปิด


1

เพียงแค่เขียนรหัสต่อไปนี้ใน onBackPressed:

@Override
public void onBackPressed() {
    // super.onBackPressed();

    //Creating an alert dialog to logout
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Do you want to Exit?");
    alertDialogBuilder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    startActivity(intent);
                }
            });

    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {

                }
            });

    //Showing the alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

0

โดยการโทรเสร็จสิ้น (); ในปุ่ม OnClick หรือบนเมนู

กรณี R.id.menu_settings:

      finish();
     return true;

ตามที่ระบุไว้ในความเห็นของคำตอบอื่น ๆfinish()ไม่ได้ฆ่าแอป มันอาจย้อนกลับไปที่เจตนาก่อนหน้าหรือพื้นหลังของแอพ
Raptor

0

ฉันคิดว่ามันจะปิดกิจกรรมของคุณและกิจกรรมย่อยทั้งหมดที่เกี่ยวข้อง

public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();]
        if (id == R.id.Exit) {
            this.finishAffinity();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

0

วิธีที่ดีที่สุดและสั้นที่สุดในการใช้ตาราง System.exit

System.exit(0);

VM หยุดการทำงานเพิ่มเติมและโปรแกรมจะออกจากโปรแกรม


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.