簡體   English   中英

通過引用呼叫者或被呼叫者在兩個Android活動之間進行通信

[英]Communication Between Two Android Activities via Reference to Caller or Callee

我在同一應用程序中有兩個活動Activity A和Activity B,它們需要能夠彼此調用方法。 A需要在開始時與B通信(可能請參見下面的“代碼”)。

B將在A上調用很多方法(這意味着我無法使用startActivityForResult方法進行通信,因為這將關閉B(Activity B是Bluetooth Client和Server,因為它是Peer to Peer應用程序))。 我確實使用startActivityForResult來啟動B,以獲得更多的終止信號,而不是結果。

活動A使用SupportMapFragment,而活動B不能是片段,因為我希望能夠從B切換到A,然后再也不使用B。

最初,我從一個活動開始,嘗試使用ViewFlipper切換視圖,而只是調用setContentView(R.layout.my_layout_A)或setContentView(R.layout.my_layout_B)。 片段當然對兩者都造成了很多問題。

使用片段令人困惑。 SupportMapFragment是作為片段的Google Map的代碼。

當我單擊MapsActivity(活動A)內部的菜單選項時,我希望能夠以對MapsActivity(活動A)的引用啟動myBluetoothActivity(活動B),或者啟動myBluetoothActivity,然后能夠設置對myBluetoothActivity內部的調用方(但此選項將需要引用MapsActivity內部的BluetoothActivity或通過某種方式從意圖中獲取啟動的活動)。

//the following code is in Kotlin, but this can easily be converted over to java:
//option A: (pass it inside of the constructor)
var mbta:myBluetoothActivity = myBluetoothActivity(this)
//line for intent that I am unsure of
//intent so that I can start the activity with the pointer to the caller already passed into the new activity
startActivity(mbta)

//option B: (set this reference after obtaining a reference from intent):
var mintent:Intent = Intent(this.applicationContext, myBluetoothActivity::class.java)
startActivity(mintent)
//obtain the reference to the BluetoothActivity from the intent (NOT SURE HOW TO DO THIS???)
mbta.setCallerReference(this)

如何通過兩個活動之間的引用來完成兩個活動之間的通信? 我應該使用接口進行通訊嗎? 如果我應該使用它(我曾嘗試過),應該怎么做?

換句話說,我試圖通過對B內部的活動A的引用直接從(活動B)訪問調用者活動(活動A),或者嘗試從在活動A內啟動它的意圖中獲取對B的引用。我正在嘗試對此進行參考,因此我可以將其用於通信/方法調用/成員變量和UI修改目的。

注意:1. BluetoothActivity和MapsActivity不可序列化。 我嘗試對其進行序列化,然后將其添加到Intent中的Extras Bundle中,它崩潰了,因為BroadCastReciever導致無法序列化。 因為這也可以處理WIFI。 我高度考慮將其與BluetoothActivity分離在將來的版本中。

  1. 我還假設,活動B絕不會由我的MapsActivity類啟動。

  2. 我也是Kotlin的新手,但我知道Java。

  3. 當我嘗試使用接口時,導致了StackOverflow錯誤,我也不知道為什么。

  4. 我已經閱讀了網站上有關Intent的文檔。

  5. 我在這里做了一些研究,這些研究都給了我以上這些想法。 我不確定如何實施它們。

您使用了錯誤的方法。 該解決方案需要比您想象的更多的工作。 正確的方法是:

首先,認識到這些活動Activity A和Activity B(以及任何其他活動)是特定於您的應用程序的活動,並且您希望在它們之間建立直接通信。

其次,意識到您正在嘗試獲取當前(或先前)活動的上下文。 上下文將有助於提供參考。

第三,您可以通過擴展所需的類來創建自己的Activity和Application類。 Application類是用於活動的低級類。

從這里,您將能夠使用getApplicationContext(),它將返回您的自定義Application類。

設計:在CustomApplication類的內部,您必須跟蹤對所需活動的引用。 從那里開始,只需將getApplicationContext()強制轉換為CustomApplication類,然后調用訪問Activity的方法。 如果要訪問創建的特定活動的某些實例,則必須將其活動轉換為“類型”。 例如:

MapsActivity mact = (MapsActivity)(((MyApplication)(this.getApplicationContext())).getCurrentActivity())

您當然必須注意,此活動必須已經創建(已經調用onCreate方法)才能返回當前活動。 當然,該活動的其他生命周期方法也一樣,因為您將創建一個baseActivity來處理這些問題,並且您還將擁有一個Application生命周期來幫助解決這個問題。

要回答這個問題:“如何在android中獲取當前的前台活動上下文?” 我轉向StackOverflow,發現用戶:gezdy的答案正是我所需要的: 如何在android中獲取當前的前台活動上下文?

(引自:GEZDY)

您應該管理活動引用。 在清單文件中添加應用程序的名稱:

  <application android:name=".MyApp" .... </application> 

您的應用程序類:

 public class MyApp extends Application { public void onCreate() { super.onCreate(); } private Activity mCurrentActivity = null; public Activity getCurrentActivity(){ return mCurrentActivity; } public void setCurrentActivity(Activity mCurrentActivity){ this.mCurrentActivity = mCurrentActivity; } } 

創建一個新的活動:

 public class MyBaseActivity extends Activity { protected MyApp mMyApp; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mMyApp = (MyApp)this.getApplicationContext(); } protected void onResume() { super.onResume(); mMyApp.setCurrentActivity(this); } protected void onPause() { clearReferences(); super.onPause(); } protected void onDestroy() { clearReferences(); super.onDestroy(); } private void clearReferences(){ Activity currActivity = mMyApp.getCurrentActivity(); if (this.equals(currActivity)) mMyApp.setCurrentActivity(null); } } 

因此,現在不必擴展活動的Activity類,而只需擴展MyBaseActivity。 現在,您可以從應用程序或活動上下文中獲取當前活動,如下所示:

 Activity currentActivity = ((MyApp)context.getApplicationContext()).getCurrentActivity(); 

(報價結束自:GEZDY)

注意:此問題的所有代碼都用Java編寫。

暫無
暫無

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

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