开发者

Passing extra Intent information from Activity to View

开发者 https://www.devze.com 2023-02-05 21:26 出处:网络
I have an activity that is launched from another activity through an intent. The intent carries an extra \"id\" information. Now, the launched activity has a custom view (actually, a extension of Line

I have an activity that is launched from another activity through an intent. The intent carries an extra "id" information. Now, the launched activity has a custom view (actually, a extension of LinearLayout class). I want to access the "id" information in the custom view. Can开发者_JAVA技巧 the activity pass that value to its contained view? Or can the view get a handle to the activity?


YES,

First you have pass id with the intent like

Intent i=new Intent(getApplicationContext(), sample.class);
i.putExtra("id", id);
startActivity();

it pass the value to sample class

here

String i=getIntent().getExtras().getSerializable("id").toString();

& you can use this id in your custom view


Yes, your custom View class can get a reference to the Activity it is contained in. Every View has a getContext() method which returns the Context the View is running in (i.e. your Activity).


make a method in your view that takes the id as a parameter, and then call that method from your activity.

0

精彩评论

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