AbhishekB
AbhishekB

Reputation: 2085

Passing Values Between Activities Via Using Intents

I want my activity to pass a value to another activity class that extends view. Can you please tell me a solution? I am new to Android Programming.

Upvotes: 2

Views: 504

Answers (5)

DAS
DAS

Reputation: 696

In the main class (A.java)

  Intent i = new Intent(A.this,B.class);
                i.putExtra("val", the value that you want to pass);
                startActivity(i);

in B.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 setContentView(R.layout.main2);
 Integer number = getIntent().getIntExtra("val", 0); 

We will get the passes value in number

Upvotes: 0

ngesh
ngesh

Reputation: 13501

Better idea is..

class CustomView extends View{
 YourData data;
public CustomView(YourData data, Context context){
this.data = data

}

}

and in Activity

new CustomView(data. this)

Upvotes: 2

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

You can pass the activity reference in the view and use that to get the appropriate values.

class MyActivity extends Activity {
    MyView v;
    int i;
    public void onCreate(){
         v = new MyView(this);
    }

} 

class MyView extends View {
    MyActivity activity;
    public MyView(MyActivity act) {
        this.activity = act;
    }

    public void someMethod() {
        int valueFromActivity = activity.i;
    }
}

Upvotes: 0

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

pass throgh constructor or getter/seller . nothing related to android . use standard java tequnique to pass data to object .

Upvotes: 1

dsafcdsge4rfdse
dsafcdsge4rfdse

Reputation: 258

you can't because intents are used to comunicate between activitys, receivers & services.

Upvotes: 0

Related Questions