开发者

Another way to pass value from one activity to another activity

Usually we pass value in android from one activity to another activity by Intent function.Is there is any another way is possible t开发者_如何学Co pass the values from one activity to another activity with out Intent function


Yes. Create a class that extends android.app.Application, then in the Manifest put this class as the name of your application

<application android:name=".ClassNameOfTheClassYouCreated"...>
    ...activities etc here...
</application>

Now, that class that you created is your application. Its lifespan is all the way as long as the application is running, it holds the activities stack and you can add some custom fields to hold your values. To get the Application instance from an Activity, hit this.getApplication().

Edit regarding fields values being reset (in response to the commend by @hackbod): Using static fields on singletons is the other way to hold global values, but I find the proposed one more elegant. As of the case you mention with resetting values, that can also happen in other contexts (like incoming phone call, orientation change), which raises the need to sanitize (or check for existence, call it whatever you like) values before using them. Always!


Information flow can be realized in various way, all you need is a way to save and retrieve your data.

You can save and then get your data which you want it to flow via static class, file storage, database, or even remote storage by using network.

Intent is the better way. Android creates it, so use it. :)


From http://developer.android.com/guide/topics/providers/content-providers.html :

Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications; there's no common storage area that all Android packages can access.

If you want to make your own data public, you have two options: You can create your own content provider (a ContentProvider subclass) or you can add the data to an existing provider — if there's one that controls the same type of data and you have permission to write to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜