Call method with MainActivity in another Activity

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Call method with MainActivity in another Activity

Postby ploki » Thu Sep 24, 2009 10:48 pm

Hello,

I have problem with startAcitvity() - how can i call method wth another class? Exaplme:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MainActivity:
  2.  
  3. ...
  4.  
  5.  
  6.  
  7. private int mVar;
  8.  
  9.  
  10.  
  11. public void ShowNewActivity(){  // <- user call this method
  12.  
  13.     Intent i = new Intent(this, NewActivity.class);
  14.  
  15.     startActivity(i);
  16.  
  17. }
  18.  
  19.  
  20.  
  21. public void setVariable(int x){
  22.  
  23.     mVar = x;
  24.  
  25. }
  26.  
  27.  
  28.  
  29. //---
  30.  
  31.  
  32.  
  33. NewActivity
  34.  
  35.  
  36.  
  37. ...
  38.  
  39. //How can i change the mVar value in this Activity? How can I call setVariable from MainActivity? If i send only Intent - i?
  40.  
  41. //ex:
  42.  
  43.     //if user Button1 click:
  44.  
  45.         WHAT.setVariable(10);
  46.  
  47.     //else
  48.  
  49.         WHAT.setVariable(5);
  50.  
  51.     finish();
  52.  
  53. ...
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Thanks!

//@down - thansk for help, it's ok!
Last edited by ploki on Mon Sep 28, 2009 7:08 pm, edited 1 time in total.
ploki
Freshman
Freshman
 
Posts: 4
Joined: Thu Sep 24, 2009 12:06 pm

Top

Postby yaj786 » Thu Sep 24, 2009 10:55 pm

Not sure what you mean, but you can pass variables like this:

Activity 1:

Intent i = new Intent(this, Activity2.class);
i.putInt("variableName", value);
startActivity(i);



Activity 2:
Intent i=getIntent();
i.getIntExtra("variableName", defValue);
yaj786
Developer
Developer
 
Posts: 33
Joined: Fri Jul 10, 2009 7:37 am

Postby wiederke » Fri Sep 25, 2009 12:17 pm

Hi,

you can also use the Application object to handle data, which is manipulated by more than one activity. Check out the example in this thread: http://www.anddev.org/constant_communic ... t7956.html


cu,

Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests