簡體   English   中英

android中同一個應用程序的多個實例

[英]multiple instance of same app in android

從圖庫應用程序內部共享圖像時如何使用已經運行的應用程序? 它總是創建已經運行的應用程序的單獨實例。 我在whatsapp應用程序中發現了同樣的問題。

是的。另一個用例是當你點擊notification.it將啟動一個新的實例,如果該應用已經在后台。

使用android:launchMode

<activity android:launchMode = ["standard" | "singleTop" | "singleTask" | "singleInstance"] ../>

所以使用docs中的"singleTop"

如果活動的實例已存在於當前任務的頂部,則系統通過調用其onNewIntent()方法將意圖路由到該實例,而不是創建活動的新實例。 活動可以多次實例化,每個實例可以屬於不同的任務,一個任務可以有多個實例(但只有當后端堆棧頂部的活動不是活動的現有實例時)。

請閱讀以下博客文章

http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

https://www.mobomo.com/2011/06/android-understanding-activity-launchmode/

編輯:在OnResume之前,它將調用OnActivityResult。 已選擇的數據

Intent intent = new Intent();

    intent.setType("image/jpeg");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(intent,
                    "Select Image From Gallery"),
                    10000);


    @Override
        protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
Uri originalUri = null;
        if (resultCode == Activity.RESULT_OK) {
    if (requestCode == 10000) {

                    if (data.getData() != null) {
                        originalUri = data.getData();
                        //originaluri is your selected image path.


                    }}}

並在我們的應用程序中獲取Intent Filter Action。

void onCreate (Bundle savedInstanceState) {
    ...
    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent); // Handle text being sent
        } else if (type.startsWith("image/")) {
            handleSendImage(intent); // Handle single image being sent
        }
    } 

http://developer.android.com/training/sharing/receive.html http://developer.android.com/training/basics/intents/filters.html

或者如果您使用的是launchmode- singleTop。 只需覆蓋onNewIntent

 @Override
    protected void onNewIntent(Intent intent) {


        super.onNewIntent(intent);

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM