簡體   English   中英

使用 Parcelable 將對象傳遞給另一個 Activity 時,如何更新對象並查看原始 Activity 中的更改?

[英]When passing an object to another Activity using Parcelable, how can I update the object and see the changes in the original Activity?

我正在 Android 中開發一個簡單的短信應用程序。 在我的 MainActivity 中,當用戶單擊與某人的對話時,我通過使用 Parcelable 的意圖發送我的自定義對話對象。 這完美地工作。 問題是,如果我更改此對象中的任何字段,則在返回 MainActivity 時,不會顯示任何更改。 據我所知, Parcelable 只是傳遞對象的副本,而不是傳遞對它的引用。 有什么辦法可以在活動中保留這些更改嗎?

我想不斷更新聊天中發送的最后一條消息。 有一個 RecyclerView.Adapter 將使用新文本更新視圖。 最后一條消息文本將在返回到我的主活動時顯示。

我無法附加圖像,因為我沒有足夠的聲望點。 但我的 MainActivity 只是一個對話列表。 我的 ChatActivity 是實際的聊天屏幕。 這是我的代碼:

public class ChatActivity extends AppCompatActivity implements Parcelable {
    //receive my custom Conversation object here, left out for brevity
    mConversation.setLastMessage("This is a new message.");
    mChatAdapter.notifyDataSetChanged();
}

返回到我的 MainActivity 后,盡管修改了 lastMessage 字段,但未顯示任何更改。 我怎樣才能做到這一點? 或者是否有另一種方法可以跨活動持續更新對象?

非常感謝任何幫助,並感謝您的時間。 對不起,如果這是一個令人困惑的問題。

您可以使用 ViewModel 來存儲和更新您的數據。 它允許您的活動/片段查看數據的更新並將其生命周期與連接的活動/片段的生命周期進行協調。

創建一個新的 ViewModel 類:

public class MessagesViewModel extends ViewModel{
    
    // Define the LiveData variable(s) you want to store/interact with
    private MutableLiveData<ArrayList<Message>> messageList = new MutableLiveData<>();
    
    public MessagesViewModel(){
        // Read your file here ...
        
        // Use the file's data to update the value(s) of your LiveData
        messageList.setValue(newMessageList);
    }
    
    // Create a method for your Activity/Fragment(s) to access the LiveData
    public LiveData<ArrayList<Message>> getMessageList(){
        return messageList;
    }
    
    // Create a method for your Activity/Fragment(s) to update the LiveData
    public void updateMessageList(ArrayList<Message> updatedMessageList){
        messageList.setValue(updatedMessageList);
    }
    
    // Called when all connected Activity/Fragment lifecycles have finished
    // (If necessary, here is where you can clean up any resources your ViewModel uses)
    @Override
    protected void onCleared(){
        // Save your data back to file here ...
        
        super.onCleared();
    }
}

從兩個活動的 onCreate() 方法中檢索您的 ViewModel:

// Retrieve your ViewModel
// (You may want to make this a member variable depending on where you may need to access the ViewModel from)
MessagesViewModel messagesViewModel = new ViewModelProvider(this).get(MessagesViewModel.class);

// Call observe() to receive the data as well as any updates to it
messagesViewModel.getMessageList().observe(this, new Observer<ArrayList<Message>>(){
    @Override
    public void onChanged(ArrayList<Message> messageList){
        // Do something with your data ...
    }
});

當您需要從 Activity 中更新數據時:

// Create your updated data ...

// Update your ViewModel with the new data
messagesViewModel.updateMessageList(updatedMessageList);

您可以在https://developer.android.com/topic/libraries/architecture/viewmodel找到有關 ViewModel 的更多信息

暫無
暫無

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

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