Aswathy
Aswathy

Reputation: 347

calling methods of one class to another : Android

I have two java files Main and New. In main i have a number methods from oncreate to surface destroyed. on clicking one of the button it goes to New. There i have called the same xml file. But no methods are working there. i want to call the oncreate method including all methods from New. Please help

 public class Main extends Activity implements  SurfaceHolder.Callback {
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {  super.onCreate(savedInstanceState);    
       setContentView(R.layout.main);
      ...............}

        buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {......}
         });

      testButton.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) 
        {
            Intent nextIntent=new Intent(getApplicationContext(),New.class);
            startActivityForResult(nextIntent,301);
        }
        });

Second class

     public class New extends Activity
    {

    @Override
    public void onCreate(Bundle savedInstanceState) {         

       super.onCreate(savedInstanceState);    
       setContentView(R.layout.main);


   }
}

Upvotes: 0

Views: 474

Answers (3)

vipul mittal
vipul mittal

Reputation: 17401

You can also create a base activity that both MainActivity and NewActivity extends and put methods in it.

For ex:

public class BaseActivity extends Activity{



   protected void onCreate(Bundle savedInstanceState) 
{  super.onCreate(savedInstanceState);    
   setContentView(R.layout.main);
  ...............}

    buttonStartCameraPreview.setOnClickListener(new Button.OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {......}
     });

  testButton.setOnClickListener(new Button.OnClickListener(){
    @Override
    public void onClick(View v) 
    {
        Intent nextIntent=new Intent(getApplicationContext(),NewActivity.class);
        startActivityForResult(nextIntent,301);
    }
    });
    }


    public class MainActivity extends BaseActivity{
    onCreate..
    {
      ...

    }
    }


    public class NewActivity extends BaseActivity{
    onCreate..
    {
      ...

    }
    }

Upvotes: 0

Nambi
Nambi

Reputation: 12042

->Intent nextIntent=new Intent(getApplicationContext(),New.class);

replace to

Intent nextIntent=new Intent(getApplicationContext(),NewActivity.class);

Upvotes: 0

Gustek
Gustek

Reputation: 3760

Your second class name is NewActivity

but Your intent is calling different class.

Upvotes: 1

Related Questions