{"id":25208277,"url":"https://github.com/1010code/android-intent","last_synced_at":"2025-10-14T19:08:07.902Z","repository":{"id":118676011,"uuid":"350202250","full_name":"1010code/android-intent","owner":"1010code","description":null,"archived":false,"fork":false,"pushed_at":"2021-03-22T05:02:35.000Z","size":349,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-14T19:05:06.706Z","etag":null,"topics":["intent","intent-bundle"],"latest_commit_sha":null,"homepage":"https://andy6804tw.github.io/2021/03/22/android-intent/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/1010code.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-03-22T04:05:18.000Z","updated_at":"2021-03-22T05:02:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"5182c60f-4b53-4821-ade8-85468f888165","html_url":"https://github.com/1010code/android-intent","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/1010code/android-intent","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fandroid-intent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fandroid-intent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fandroid-intent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fandroid-intent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/1010code","download_url":"https://codeload.github.com/1010code/android-intent/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fandroid-intent/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279020641,"owners_count":26086895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["intent","intent-bundle"],"created_at":"2025-02-10T12:19:13.626Z","updated_at":"2025-10-14T19:08:07.894Z","avatar_url":"https://github.com/1010code.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# android-intent\n## Intent 跳頁＆傳遞資料\n\n\u003cimg src=\"./screenshot/demo.gif\" width=\"250px\"\u003e\n\n## 前言\n當 APP 有多個頁面就必需採用 Intent 來實現多個 activity 之間的跳換。當然除了頁面跳換，也能同時將值傳遞過去到新的 activity。本文章就來教各位如何實作兩個 activity 頁面的跳轉，以及傳遞數值。\n\n## Intent 用法\n我們可以透過建立一個 Intent 實例，裡面的初始值方別放入目前的頁面所在地以及欲跳轉過去的頁面。給予初始值後完後， `startActivity()` 即可啟動跳轉。\n\n```java\nIntent intent = new Intent(MainActivity.this, MainActivity2.class);\nstartActivity(intent);\n```\n\n## Intent 夾帶資料\n假設我們要傳遞浮點數(float) weight 值，可直接使用 Intent 類別所提供的 `putExtra` 方法將數值夾帶。\n\n```java\nIntent intent = new Intent(MainActivity.this, MainActivity2.class);\nfloat weight=55;\nintent.putExtra(\"WEIGHT_VALUE\", weight);\nstartActivity(intent);\n```\n\n## Intent 取值\n傳值過去後，我們可以在轉換Activity後的目的地(Main2Activity)取得在前一個Activity己放入的資料。由於資料是放在 Intent 中，因此必須先呼叫 Activity 中的 `getIntent()` 方法來取得 Intent 物件。接下來再使用 Intent 類別的 `getXXExtra()` 方法取得其中所附帶的資料，如本例之前放入的是float型態的資料，在此就應使用 `getFloatExtra()` 方法。第一個參數為取得資料的標籤。第二個為預設初始值，假設無法取得該筆資料就會回傳預設值。\n\n```java\nIntent intent = getIntent();\nfloat weight = intent.getFloatExtra(\"WEIGHT_VALUE\", 0);\n```\n\nIntent類別提供了以下幾個常用取得資料的方法：\n\n| 方法名稱                | 說明                         |\n|-------------------------|------------------------------|\n| getFloatExtra           | 取得float型態資料            |\n| getIntExtra             | 取得int型態資料              |\n| getLongExtra            | 取得long型態資料             |\n| getBooleanExtra         | 取得boolean型態資料          |\n| getCharExtra            | 取得char型態資料             |\n| getStringExtra          | 取得String型態資料           |\n| getStringArrayExtra     | 取得String陣列型態資料       |\n| getStringArrayListExtra | 取得內含String的List集合資料 |\n\n## 補充\n如果我們使用 Intent 方式傳遞值，假設需要將資料從頁面A傳遞到B，然後再傳遞到C。我們可以發現在B的頁面我們需要將這些值全部讀取出來再透過 `putExtra` 的方式將資料逐一塞進去傳到C頁面。這樣看起來會非常麻煩。而使用 `Bundle` 的話，在B頁面可以直接取出傳輸的Bundle物件然後傳輸給C頁面。\n\n如果有多種類的資料需要傳遞，可使用Bundle類別。他可以像是包裹一樣把所有數值包裝起來再透過 Intent 傳送打包出去。使用方式如下：\n\n```java\nIntent intent = new Intent(MainActivity2.this, MainActivity3.class);\nBundle bundle = new Bundle();\nbundle.putFloat(\"WEIGHT_VALUE\",weight);\nbundle.putFloat(\"HEIGHT_VALUE\", height);\nbundle.putFloat(\"BMI_VALUE\", bmi);\nintent.putExtras(bundle);\nstartActivity(intent);\n```\n\n透過 Intent 取得 Bundle 物件內容:\n\n```java\nIntent intent = getIntent();\nfloat weight = intent.getExtras().getFloat(\"WEIGHT_VALUE\", 0);\nfloat height = intent.getExtras().getFloat(\"HEIGHT_VALUE\", 0);\nfloat bmi = intent.getExtras().getFloat(\"BMI_VALUE\", 0);\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1010code%2Fandroid-intent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F1010code%2Fandroid-intent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1010code%2Fandroid-intent/lists"}