简体   繁体   中英

How can I get data back from an activity as well as Fragments?

I have two activities:

  • SetActivity.java
  • GetActivity.java

On SetActivity.java , I've loaded a fragment Frag1.

I can load Frag2 using a button of Frag1, but as I pressed the back button, I need to get data returned from Frag2 to Frag1 or GetActivity.java to SetActivity.java.

How can I do this?

SetActivity.java -> Frag1.java(send data) -> Frag2.java(get data) -> Frag1.java(get updated data from Frag2.java)

SetActivity.java (set data) -> GetActivity.java (get data) -> SetActivity.java (Get updated data back)

How to do this?

Use Intents to do that.

You can pass data using intent.putExtra in the first fragment and receive it by intent.getExtra in the second fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM