簡體   English   中英

如何將地圖數據從活動傳遞到片段?

[英]how to pass map data from an activity to a fragment?

我的主要活動有一個片段(firstFragment)。 在此片段中,我有一個打開地圖活動的地圖按鈕。 在此地圖活動中,我將點的緯度和經度捆綁在一起,然后完成活動並返回至第一片段。 在第一片段中,我想獲得捆綁包並顯示緯度。 我怎樣才能做到這一點?

指官方api:

http://developer.android.com/training/basics/intents/result.html

你應該使用方法

 startActivityForResult()

在片段上。

用法示例:

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

並實現此方法:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}

以及在地圖活動中這樣的代碼,在您將其關閉的代碼中調用了“ onStop”,“ onCancel”,“ onbackPressed”,無論您用什么方法關閉它:

private void finishWithResult()
{
    Bundle conData = new Bundle();
    conData.putFloat("lat", 0.0);
 conData.putFloat("lon", 0.0);
    Intent intent = new Intent();
    intent.putExtras(conData);
    setResult(RESULT_OK, intent);
    finish();
}

暫無
暫無

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

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