简体   繁体   中英

how to pass value in multiple activites

my android app activity sequence is activity1->activity2->activity3->activity4 . I can pass activity1 X value to activity2 using intent.putExtra() its works fine.

But if i want to access my activity1 X value in activity4 , i don't want to use X in activity2 and activity3 . But can't change the sequence. do i have to pass one by one each activity or is there any other way?

There's always a 'path' of Activities that you can use to pass objects around. But that's cumbersome. As mentioned you can use Sharedpreferences, that's cumbersome too. You can use global variables or the Singleton pattern, some consider this not very elegant but it's okay for some simple stuff. I'd look into EventBus , especially the sticky events. It works, simply put, like the mail. Activity 1 puts something into the mailbox, Activity 2 either gets it delivered to it's front door (normal event, delivered instantly) or can go pick it up at the mailbox (sticky event). This also has it's drawbacks, but I'd not bother about them until you run into them.

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