Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call a method in another Activity from Activity

Tags:

I'm developing an Android Application in which I have

I've two classes class A and Class B.

In class A, I tried the code Snippets like below,

How do I call a method in another Activity from Activity?

public class FirstActivity extends Activity {        public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main2);     }            public void method() {         // some code     }   }  public class SecondActivity extends Activity {        public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          setContentView(R.layout.main2);              FirstActivity fact = new FIrstActivity();              fact.method();     } } 
like image 253
user2932841 Avatar asked Oct 29 '13 18:10

user2932841


People also ask

How can we call method in activity from non activity class?

onCreate(savedInstanceState); setContentView(R. layout. main2); DataClass dc = new DataClass(); dc. show(); } public void call(ArrayList<String> arr) { // Some code... } }


2 Answers

The startActivityForResult pattern is much better suited for what you're trying to achieve : http://developer.android.com/reference/android/app/Activity.html#StartingActivities

Try below code

public class MainActivity extends Activity {        Button button1;       @Override       protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.activity_main);           textView1=(TextView)findViewById(R.id.textView1);           button1=(Button)findViewById(R.id.button1);           button1.setOnClickListener(new OnClickListener() {               @Override               public void onClick(View arg0) {                   Intent intent=new Intent(MainActivity.this,SecondActivity.class);                   startActivityForResult(intent, 2);// Activity is started with requestCode 2               }           });       }    // Call Back method  to get the Message form other Activity       @Override          protected void onActivityResult(int requestCode, int resultCode, Intent data)          {                    super.onActivityResult(requestCode, resultCode, data);                     // check if the request code is same as what is passed  here it is 2                      if(requestCode==2)                            {                             //do the things u wanted                           }        }    }  

SecondActivity.class

public class SecondActivity extends Activity {        Button button1;       @Override       protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.activity_second);                button1=(Button)findViewById(R.id.button1);               button1.setOnClickListener(new OnClickListener() {                   @Override                   public void onClick(View arg0) {                       String message="hello ";                       Intent intent=new Intent();                       intent.putExtra("MESSAGE",message);                       setResult(2,intent);                       finish();//finishing activity                   }               });       }    }   

Let me know if it helped...

like image 121
Rissmon Suresh Avatar answered Oct 19 '22 09:10

Rissmon Suresh


You should not create an instance of the activity class. It is wrong. Activity has ui and lifecycle and activity is started by startActivity(intent)

You can use startActivityForResult or you can pass the values from one activity to another using intents and do what is required. But it depends on what you intend to do in the method.

like image 38
Raghunandan Avatar answered Oct 19 '22 09:10

Raghunandan